description: You are given two arrays of integers a_1, a_2, …, a_n and b_1, b_2, …, b_m.
  
  You need to insert all elements of b into a in an arbitrary way. As a result you will get an array c_1, c_2, …, c_{n+m} of size n + m.
  
  Note that you are not allowed to change the order of elements in a, while you can insert elements of b at arbitrary positions. They can be inserted at the beginning, between any elements of a, or at the end. Moreover, elements of b can appear in the resulting array in any order.
  
  What is the minimum possible number of inversions in the resulting array c? Recall that an inversion is a pair of indices (i, j) such that i < j and c_i > c_j.
  
  Input
  
  Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^4). Description of the test cases follows.
  
  The first line of each test case contains two integers n and m (1 ≤ n, m ≤ 10^6).
  
  The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9).
  
  The third line of each test case contains m integers b_1, b_2, …, b_m (1 ≤ b_i ≤ 10^9).
  
  It is guaranteed that the sum of n for all tests cases in one input doesn't exceed 10^6. The sum of m for all tests cases doesn't exceed 10^6 as well.
  
  Output
  
  For each test case, print one integer — the minimum possible number of inversions in the resulting array c.
  
  Example
  
  Input
  
  
  3
  3 4
  1 2 3
  4 3 2 1
  3 3
  3 2 1
  1 2 3
  5 4
  1 3 5 3 1
  4 3 6 1
  
  
  Output
  
  
  0
  4
  6
  
  Note
  
  Below is given the solution to get the optimal answer for each of the example test cases (elements of a are underscored).
  
    * In the first test case, c = [\underline{1}, 1, \underline{2}, 2, \underline{3}, 3, 4]. 
    * In the second test case, c = [1, 2, \underline{3}, \underline{2}, \underline{1}, 3]. 
    * In the third test case, c = [\underline{1}, 1, 3, \underline{3}, \underline{5}, \underline{3}, \underline{1}, 4, 6]. 
solution: #include <bits/stdc++.h>
  using namespace std;
  const int max_n = 2000111, inf = 1000111222;
  int a[max_n];
  int b[max_n];
  int n, m;
  vector<int> ccc;
  void clr() { ccc.clear(); }
  int get_small(int large) {
    return lower_bound(ccc.begin(), ccc.end(), large) - ccc.begin();
  }
  struct segment_tree {
    int sum[max_n * 4];
    int mn[max_n * 4];
    void build(int cur, int l, int r) {
      if (l == r) {
        mn[cur] = 0;
        sum[cur] = 1;
        return;
      }
      int mid = (l + r) / 2;
      build(cur * 2, l, mid);
      build(cur * 2 + 1, mid + 1, r);
      sum[cur] = sum[cur * 2] + sum[cur * 2 + 1];
      mn[cur] = min(mn[cur * 2], sum[cur * 2] + mn[cur * 2 + 1]);
    }
    void upd(int cur, int l, int r, int pos, int val) {
      if (l == r) {
        sum[cur] = val;
        if (val < 0)
          mn[cur] = val;
        else
          mn[cur] = 0;
        return;
      }
      int mid = (l + r) / 2;
      if (pos <= mid) {
        upd(cur * 2, l, mid, pos, val);
      } else {
        upd(cur * 2 + 1, mid + 1, r, pos, val);
      }
      sum[cur] = sum[cur * 2] + sum[cur * 2 + 1];
      mn[cur] = min(mn[cur * 2], sum[cur * 2] + mn[cur * 2 + 1]);
    }
  } st;
  int ft[2 * max_n];
  void add(int pos, int val) {
    for (int i = pos; i < n + m; i |= (i + 1)) {
      ft[i] += val;
    }
  }
  int get(int pos) {
    int ans = 0;
    for (int i = pos; i >= 0; i = (i & (i + 1)) - 1) {
      ans += ft[i];
    }
    return ans;
  }
  int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int T;
    cin >> T;
    while (T--) {
      cin >> n >> m;
      clr();
      for (int i = 0; i < n; ++i) {
        cin >> a[i];
        ccc.push_back(a[i]);
      }
      for (int i = 0; i < m; ++i) {
        cin >> b[i];
        ccc.push_back(b[i]);
      }
      st.build(1, 0, n + m - 1);
      sort(ccc.begin(), ccc.end());
      ccc.erase(unique(ccc.begin(), ccc.end()), ccc.end());
      vector<pair<int, int>> v;
      for (int i = 0; i < n + m; ++i) {
        ft[i] = 0;
      }
      long long ans2 = 0;
      for (int i = 0; i < n; ++i) {
        a[i] = get_small(a[i]);
        ans2 += (i - get(a[i]));
        add(a[i], 1);
        v.emplace_back(a[i], i);
      }
      for (int i = 0; i < m; ++i) {
        b[i] = get_small(b[i]);
      }
      long long ans = 0;
      sort(b, b + m);
      int cnt_less = 0;
      sort(v.begin(), v.end());
      reverse(v.begin(), v.end());
      vector<pair<int, int>> v2;
      for (int i = 0; i < m; ++i) {
        int cur = b[i];
        while (!v2.empty() && v2.back().first < cur) {
          st.upd(1, 0, n + m - 1, v2.back().second, -1);
          v2.pop_back();
          ++cnt_less;
        }
        while (!v.empty() && v.back().first < cur) {
          st.upd(1, 0, n + m - 1, v.back().second, -1);
          v.pop_back();
          ++cnt_less;
        }
        while (!v.empty() && v.back().first == cur) {
          st.upd(1, 0, n + m - 1, v.back().second, 0);
          v2.push_back(v.back());
          v.pop_back();
        }
        ans += cnt_less + st.mn[1];
      }
      cout << ans2 + ans << "\n";
    }
    return 0;
  }
  