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;
  using ll = long long;
  using ld = long double;
  using str = string;
  using pi = pair<int, int>;
  using pl = pair<ll, ll>;
  using vi = vector<int>;
  using vl = vector<ll>;
  using vpi = vector<pair<int, int>>;
  using vvi = vector<vi>;
  int Bit(int mask, int b) { return (mask >> b) & 1; }
  template <class T>
  bool ckmin(T &a, const T &b) {
    if (b < a) {
      a = b;
      return true;
    }
    return false;
  }
  template <class T>
  bool ckmax(T &a, const T &b) {
    if (b > a) {
      a = b;
      return true;
    }
    return false;
  }
  const ll INF = 2e18;
  struct SegTree {
    struct Node {
      ll mn;
      ll upd;
      Node() : mn(INF), upd(0) {}
      void combine(const Node &a, const Node &b) { mn = min(a.mn, b.mn) + upd; }
      void check(ll x) {
        mn = x;
        upd = 0;
      }
      void update(ll x) {
        upd += x;
        mn += x;
      }
    };
    vector<Node> t;
    int n;
    void build(int sz) {
      n = 2;
      while (n < sz) n *= 2;
      t.resize(n * 2 + 5);
      for (int i = 0; i < (sz); ++i) {
        t[i + n].check(i);
      }
      for (int i = n - 1; i >= 1; --i) {
        t[i].combine(t[i << 1], t[i << 1 | 1]);
      }
    }
    void upd(int v, int l, int r, int lq, int rq, ll x) {
      if (lq >= r || l >= rq) return;
      if (lq <= l && r <= rq) {
        t[v].update(x);
        return;
      }
      upd(v << 1, l, (l + r) >> 1, lq, rq, x);
      upd(v << 1 | 1, (l + r) >> 1, r, lq, rq, x);
      t[v].combine(t[v << 1], t[v << 1 | 1]);
    }
    void upd(int l, int r, ll x) { upd(1, 0, n, l, r, x); }
    Node get(int v, int l, int r, int lq, int rq) {
      Node result;
      if (l >= rq || lq >= r) return result;
      if (lq <= l && r <= rq) {
        result = t[v];
        return result;
      }
      auto L = get(v << 1, l, (l + r) >> 1, lq, rq);
      auto R = get(v << 1 | 1, (l + r) >> 1, r, lq, rq);
      result.combine(L, R);
      result.update(t[v].upd);
      return result;
    }
    Node get(int lq, int rq) { return get(1, 0, n, lq, rq); }
  };
  struct fenwick {
    int n{};
    vector<ll> fenw{};
    void build(int k) {
      n = k;
      fenw.resize(n);
    }
    void upd(int i, ll x) {
      for (; i < n; i = i | (i + 1)) fenw[i] += x;
    }
    ll get(int i) {
      ll res = 0;
      for (; i >= 0; i = (i & (i + 1)) - 1) res += fenw[i];
      return res;
    }
    ll get(int l, int r) {
      if (l > r) return 0;
      return get(r) - get(l - 1);
    }
  };
  void solve() {
    int n, m;
    cin >> n >> m;
    vi a(n), b(m);
    for (int i = 0; i < (n); ++i) cin >> a[i];
    for (int i = 0; i < (m); ++i) cin >> b[i];
    sort((b).begin(), (b).end());
    SegTree st;
    st.build(n + 1);
    int l = 0, r = 0;
    vector<pair<int, int>> c(n);
    for (int i = 0; i < (n); ++i) c[i] = {a[i], i};
    sort((c).begin(), (c).end());
    ll ans = 0;
    for (int i = 0; i < (m); ++i) {
      while (r < n && c[r].first <= b[i]) {
        st.upd(c[r].second + 1, n + 1, -1);
        r++;
      }
      while (l < n && c[l].first < b[i]) {
        st.upd(c[l].second + 1, n + 1, -1);
        l++;
      }
      ll cur = l;
      cur += st.get(0, n + 1).mn;
      ans += cur;
    }
    ll ans2 = 0;
    fenwick f;
    f.build(n);
    for (int i = 0, j = 0; i < n; i = j) {
      while (j < n && c[j].first == c[i].first) j++;
      for (int e = i; e < j; ++e) {
        ans2 += f.get(c[e].second + 1, n - 1);
      }
      for (int e = i; e < j; ++e) {
        f.upd(c[e].second, 1);
      }
    }
    cout << ans + ans2 << '\n';
  }
  int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    cin >> t;
    for (int i = 0; i < (t); ++i) {
      solve();
    }
    return 0;
  }
  