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;
  vector<int> seg(1000050 * 12, 0);
  vector<int> lazy(1000050 * 12, 0);
  void update(int node, int start, int end, int rangemin, int rangemax, int val) {
    if (lazy[node] != 0) {
      seg[node] += lazy[node];
      lazy[node * 2 + 1] += lazy[node];
      lazy[node * 2 + 2] += lazy[node];
      lazy[node] = 0;
    }
    if (start >= rangemin && end <= rangemax) {
      seg[node] += val;
      lazy[node * 2 + 1] += val;
      lazy[node * 2 + 2] += val;
      return;
    }
    if (start > rangemax || end < rangemin) {
      return;
    }
    int mid = (start + end) / 2;
    update(node * 2 + 1, start, mid, rangemin, rangemax, val);
    update(node * 2 + 2, mid + 1, end, rangemin, rangemax, val);
    seg[node] = min(seg[node * 2 + 1], seg[node * 2 + 2]);
  }
  int query(int node, int start, int end, int rangemin, int rangemax) {
    if (lazy[node] != 0) {
      seg[node] += lazy[node];
      lazy[node * 2 + 1] += lazy[node];
      lazy[node * 2 + 2] += lazy[node];
      lazy[node] = 0;
    }
    if (start >= rangemin || end <= rangemax) {
      return seg[node];
    }
    if (start > rangemax || end < rangemin) {
      return -1;
    }
    int mid = (start + end) / 2;
    return min(query(node * 2 + 1, start, mid, rangemin, rangemax),
               query(node * 2 + 2, mid + 1, end, rangemin, rangemax));
  }
  void update(int b, int c, vector<int> &fenwick) {
    int temp = b + 1;
    while (temp < fenwick.size()) {
      fenwick[temp] += c;
      temp += temp & (-temp);
    }
  }
  int getsum(int a, int b, vector<int> &fenwick) {
    int temp = b + 1;
    int total = 0;
    while (temp != 0) {
      total += fenwick[temp];
      temp -= temp & (-temp);
    }
    int idk = a;
    while (idk != 0) {
      total -= fenwick[idk];
      idk -= idk & (-idk);
    }
    return total;
  }
  long long solve(vector<int> vect1) {
    vector<int> fenwick(vect1.size() + 5, 0);
    int n = vect1.size();
    vector<int> compress = vect1;
    sort(compress.begin(), compress.end());
    for (int i = 0; i < vect1.size(); i++) {
      vect1[i] = 1 + lower_bound(compress.begin(), compress.end(), vect1[i]) -
                 compress.begin();
    }
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      ans += getsum(vect1[i] + 1, n + 2, fenwick);
      update(vect1[i], 1, fenwick);
    }
    return ans;
  }
  void find() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < 5 * (n); i++) {
      seg[i] = 0;
      lazy[i] = 0;
    }
    int input;
    vector<pair<int, int> > vect1 = {{-1, -1}};
    vector<int> temp;
    vector<int> vect2;
    for (int i = 0; i < n; i++) {
      cin >> input;
      vect1.push_back({input, i});
      temp.push_back(input);
      update(0, 0, n, 0, i, 1);
    }
    for (int i = 0; i < m; i++) {
      cin >> input;
      vect2.push_back(input);
    }
    sort(vect1.begin(), vect1.end());
    reverse(vect1.begin(), vect1.end());
    sort(vect2.begin(), vect2.end());
    reverse(vect2.begin(), vect2.end());
    vect2.push_back(-1);
    int curr = 0;
    long long ans = 0;
    deque<int> q1;
    for (int i = 0; i < vect2.size() - 1; i++) {
      while (vect1[curr].first >= vect2[i]) {
        update(0, 0, n, 0, vect1[curr].second, -1);
        if (vect1[curr].first != vect2[i]) {
          update(0, 0, n, vect1[curr].second + 1, n, 1);
        } else {
          q1.push_back(vect1[curr].second);
        }
        curr++;
      }
      int mini = query(0, 0, n, 0, n);
      ans += mini;
      if (vect2[i] != vect2[i + 1]) {
        for (auto k : q1) {
          update(0, 0, n, k + 1, n, 1);
        }
        q1.clear();
      }
    }
    cout << ans + solve(temp) << "\n";
  }
  int32_t main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
      find();
    }
  }
  