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: import java.io.OutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.stream.IntStream;
  import java.util.Arrays;
  import java.util.Random;
  import java.io.OutputStream;
  import java.io.PrintStream;
  import java.io.IOException;
  import java.lang.reflect.Field;
  import java.nio.charset.StandardCharsets;
  import java.io.UncheckedIOException;
  import java.io.Closeable;
  import java.io.InputStream;
  
  /**
   * Built using CHelper plug-in
   * Actual solution is at the top
   */
  public class Main {
      public static void main(String[] args) throws Exception {
          Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 29);
          thread.start();
          thread.join();
      }
  
      static class TaskAdapter implements Runnable {
          @Override
          public void run() {
              InputStream inputStream = System.in;
              OutputStream outputStream = System.out;
              FastInput in = new FastInput(inputStream);
              FastOutput out = new FastOutput(outputStream);
              COptimalInsertion solver = new COptimalInsertion();
              int testCount = Integer.parseInt(in.next());
              for (int i = 1; i <= testCount; i++)
                  solver.solve(i, in, out);
              out.close();
          }
      }
  
      static class COptimalInsertion {
          Debug debug = new Debug(false);
          int[] a;
          int[] cnt;
          int[] b;
          int[] assign;
  
          public void solve(int testNumber, FastInput in, FastOutput out) {
              int n = in.ri();
              int m = in.ri();
              a = in.ri(n);
              b = in.ri(m);
              assign = new int[m];
              SortUtils.radixSort(b, 0, m - 1);
              cnt = new int[n + 1];
              dac(0, n, 0, m - 1);
              IntegerArrayList list = new IntegerArrayList(n + m);
              int iter = 0;
              for (int i = 0; i < m; i++) {
                  while (iter < assign[i]) {
                      list.add(a[iter++]);
                  }
                  list.add(b[i]);
              }
              while (iter < n) {
                  list.add(a[iter++]);
              }
              debug.debug("list", list);
              long ans = InversePair.inversePairCount(list.toArray());
              out.println(ans);
          }
  
          public void dac(int l, int r, int L, int R) {
              if (L > R) {
                  return;
              }
              int M = (L + R) / 2;
              Arrays.fill(cnt, l, r + 1, 0);
              int ps = 0;
              for (int i = l; i < r; i++) {
                  if (a[i] > b[M]) {
                      ps++;
                  }
                  cnt[i + 1] += ps;
              }
              ps = 0;
              for (int i = r - 1; i >= l; i--) {
                  if (a[i] < b[M]) {
                      ps++;
                  }
                  cnt[i] += ps;
              }
              int bestChoice = l;
              for (int i = l; i <= r; i++) {
                  if (cnt[i] < cnt[bestChoice]) {
                      bestChoice = i;
                  }
              }
              assign[M] = bestChoice;
              dac(l, bestChoice, L, M - 1);
              dac(bestChoice, r, M + 1, R);
          }
  
      }
  
      static class FastInput {
          private final InputStream is;
          private StringBuilder defaultStringBuf = new StringBuilder(1 << 13);
          private byte[] buf = new byte[1 << 13];
          private int bufLen;
          private int bufOffset;
          private int next;
  
          public FastInput(InputStream is) {
              this.is = is;
          }
  
          public void populate(int[] data) {
              for (int i = 0; i < data.length; i++) {
                  data[i] = readInt();
              }
          }
  
          private int read() {
              while (bufLen == bufOffset) {
                  bufOffset = 0;
                  try {
                      bufLen = is.read(buf);
                  } catch (IOException e) {
                      bufLen = -1;
                  }
                  if (bufLen == -1) {
                      return -1;
                  }
              }
              return buf[bufOffset++];
          }
  
          public void skipBlank() {
              while (next >= 0 && next <= 32) {
                  next = read();
              }
          }
  
          public String next() {
              return readString();
          }
  
          public int ri() {
              return readInt();
          }
  
          public int[] ri(int n) {
              int[] ans = new int[n];
              populate(ans);
              return ans;
          }
  
          public int readInt() {
              boolean rev = false;
  
              skipBlank();
              if (next == '+' || next == '-') {
                  rev = next == '-';
                  next = read();
              }
  
              int val = 0;
              while (next >= '0' && next <= '9') {
                  val = val * 10 - next + '0';
                  next = read();
              }
  
              return rev ? val : -val;
          }
  
          public String readString(StringBuilder builder) {
              skipBlank();
  
              while (next > 32) {
                  builder.append((char) next);
                  next = read();
              }
  
              return builder.toString();
          }
  
          public String readString() {
              defaultStringBuf.setLength(0);
              return readString(defaultStringBuf);
          }
  
      }
  
      static class RandomWrapper {
          private Random random;
          public static final RandomWrapper INSTANCE = new RandomWrapper();
  
          public RandomWrapper() {
              this(new Random());
          }
  
          public RandomWrapper(Random random) {
              this.random = random;
          }
  
          public RandomWrapper(long seed) {
              this(new Random(seed));
          }
  
          public int nextInt(int l, int r) {
              return random.nextInt(r - l + 1) + l;
          }
  
      }
  
      static class InversePair {
          public static long inversePairCount(int[] a) {
              int n = a.length;
              int[] indices = IntStream.range(0, n).toArray();
              SortUtils.quickSort(indices, (x, y) -> a[x] == a[y] ? Integer.compare(x, y) :
                      Integer.compare(a[x], a[y]), 0, n);
              long ans = 0;
              IntegerBIT bit = new IntegerBIT(n);
              for (int i : indices) {
                  ans += bit.query(i + 1, n);
                  bit.update(i + 1, 1);
              }
              return ans;
          }
  
      }
  
      static class FastOutput implements AutoCloseable, Closeable, Appendable {
          private static final int THRESHOLD = 32 << 10;
          private OutputStream writer;
          private StringBuilder cache = new StringBuilder(THRESHOLD * 2);
          private static Field stringBuilderValueField;
          private char[] charBuf = new char[THRESHOLD * 2];
          private byte[] byteBuf = new byte[THRESHOLD * 2];
  
          static {
              try {
                  stringBuilderValueField = StringBuilder.class.getSuperclass().getDeclaredField("value");
                  stringBuilderValueField.setAccessible(true);
              } catch (Exception e) {
                  stringBuilderValueField = null;
              }
              stringBuilderValueField = null;
          }
  
          public FastOutput append(CharSequence csq) {
              cache.append(csq);
              return this;
          }
  
          public FastOutput append(CharSequence csq, int start, int end) {
              cache.append(csq, start, end);
              return this;
          }
  
          private void afterWrite() {
              if (cache.length() < THRESHOLD) {
                  return;
              }
              flush();
          }
  
          public FastOutput(OutputStream writer) {
              this.writer = writer;
          }
  
          public FastOutput append(char c) {
              cache.append(c);
              afterWrite();
              return this;
          }
  
          public FastOutput append(long c) {
              cache.append(c);
              afterWrite();
              return this;
          }
  
          public FastOutput println(long c) {
              return append(c).println();
          }
  
          public FastOutput println() {
              return append('\n');
          }
  
          public FastOutput flush() {
              try {
                  if (stringBuilderValueField != null) {
                      try {
                          byte[] value = (byte[]) stringBuilderValueField.get(cache);
                          writer.write(value, 0, cache.length());
                      } catch (Exception e) {
                          stringBuilderValueField = null;
                      }
                  }
                  if (stringBuilderValueField == null) {
                      int n = cache.length();
                      if (n > byteBuf.length) {
                          //slow
                          writer.write(cache.toString().getBytes(StandardCharsets.UTF_8));
  //                writer.append(cache);
                      } else {
                          cache.getChars(0, n, charBuf, 0);
                          for (int i = 0; i < n; i++) {
                              byteBuf[i] = (byte) charBuf[i];
                          }
                          writer.write(byteBuf, 0, n);
                      }
                  }
                  writer.flush();
                  cache.setLength(0);
              } catch (IOException e) {
                  throw new UncheckedIOException(e);
              }
              return this;
          }
  
          public void close() {
              flush();
              try {
                  writer.close();
              } catch (IOException e) {
                  throw new UncheckedIOException(e);
              }
          }
  
          public String toString() {
              return cache.toString();
          }
  
      }
  
      static interface IntegerComparator {
          public int compare(int a, int b);
  
      }
  
      static class Debug {
          private boolean offline;
          private PrintStream out = System.err;
          static int[] empty = new int[0];
  
          public Debug(boolean enable) {
              offline = enable && System.getSecurityManager() == null;
          }
  
          public Debug debug(String name, Object x) {
              return debug(name, x, empty);
          }
  
          public Debug debug(String name, Object x, int... indexes) {
              if (offline) {
                  if (x == null || !x.getClass().isArray()) {
                      out.append(name);
                      for (int i : indexes) {
                          out.printf("[%d]", i);
                      }
                      out.append("=").append("" + x);
                      out.println();
                  } else {
                      indexes = Arrays.copyOf(indexes, indexes.length + 1);
                      if (x instanceof byte[]) {
                          byte[] arr = (byte[]) x;
                          for (int i = 0; i < arr.length; i++) {
                              indexes[indexes.length - 1] = i;
                              debug(name, arr[i], indexes);
                          }
                      } else if (x instanceof short[]) {
                          short[] arr = (short[]) x;
                          for (int i = 0; i < arr.length; i++) {
                              indexes[indexes.length - 1] = i;
                              debug(name, arr[i], indexes);
                          }
                      } else if (x instanceof boolean[]) {
                          boolean[] arr = (boolean[]) x;
                          for (int i = 0; i < arr.length; i++) {
                              indexes[indexes.length - 1] = i;
                              debug(name, arr[i], indexes);
                          }
                      } else if (x instanceof char[]) {
                          char[] arr = (char[]) x;
                          for (int i = 0; i < arr.length; i++) {
                              indexes[indexes.length - 1] = i;
                              debug(name, arr[i], indexes);
                          }
                      } else if (x instanceof int[]) {
                          int[] arr = (int[]) x;
                          for (int i = 0; i < arr.length; i++) {
                              indexes[indexes.length - 1] = i;
                              debug(name, arr[i], indexes);
                          }
                      } else if (x instanceof float[]) {
                          float[] arr = (float[]) x;
                          for (int i = 0; i < arr.length; i++) {
                              indexes[indexes.length - 1] = i;
                              debug(name, arr[i], indexes);
                          }
                      } else if (x instanceof double[]) {
                          double[] arr = (double[]) x;
                          for (int i = 0; i < arr.length; i++) {
                              indexes[indexes.length - 1] = i;
                              debug(name, arr[i], indexes);
                          }
                      } else if (x instanceof long[]) {
                          long[] arr = (long[]) x;
                          for (int i = 0; i < arr.length; i++) {
                              indexes[indexes.length - 1] = i;
                              debug(name, arr[i], indexes);
                          }
                      } else {
                          Object[] arr = (Object[]) x;
                          for (int i = 0; i < arr.length; i++) {
                              indexes[indexes.length - 1] = i;
                              debug(name, arr[i], indexes);
                          }
                      }
                  }
              }
              return this;
          }
  
      }
  
      static class SortUtils {
          private static final int THRESHOLD = 8;
          private static final int[] BUF8 = new int[1 << 8];
          private static final IntegerArrayList INT_LIST_A = new IntegerArrayList();
  
          private SortUtils() {
          }
  
          public static void insertSort(int[] data, IntegerComparator cmp, int l, int r) {
              for (int i = l + 1; i <= r; i++) {
                  int j = i;
                  int val = data[i];
                  while (j > l && cmp.compare(data[j - 1], val) > 0) {
                      data[j] = data[j - 1];
                      j--;
                  }
                  data[j] = val;
              }
          }
  
          public static void quickSort(int[] data, IntegerComparator cmp, int f, int t) {
              if (t - f <= THRESHOLD) {
                  insertSort(data, cmp, f, t - 1);
                  return;
              }
              SequenceUtils.swap(data, f, RandomWrapper.INSTANCE.nextInt(f, t - 1));
              int l = f;
              int r = t;
              int m = l + 1;
              while (m < r) {
                  int c = cmp.compare(data[m], data[l]);
                  if (c == 0) {
                      m++;
                  } else if (c < 0) {
                      SequenceUtils.swap(data, l, m);
                      l++;
                      m++;
                  } else {
                      SequenceUtils.swap(data, m, --r);
                  }
              }
              quickSort(data, cmp, f, l);
              quickSort(data, cmp, m, t);
          }
  
          public static void radixSort(int[] data, int l, int r) {
              INT_LIST_A.clear();
              INT_LIST_A.ensureSpace(r - l + 1);
  
              int n = r - l + 1;
              for (int i = 0; i < 8; i += 2) {
                  radixSort0(data, l, r, INT_LIST_A.getData(), 0, BUF8, i * 8);
                  radixSort0(INT_LIST_A.getData(), 0, n - 1, data, l, BUF8, (i + 1) * 8);
              }
          }
  
          private static void radixSort0(int[] data, int dl, int dr, int[] output, int ol, int[] buf, int rightShift) {
              Arrays.fill(buf, 0);
              int mask = buf.length - 1;
              for (int i = dl; i <= dr; i++) {
                  buf[(int) ((data[i] >>> rightShift) & mask)]++;
              }
              for (int i = 1; i < buf.length; i++) {
                  buf[i] += buf[i - 1];
              }
              for (int i = dr; i >= dl; i--) {
                  output[ol + (--buf[(int) ((data[i] >>> rightShift) & mask)])] = data[i];
              }
          }
  
      }
  
      static class IntegerArrayList implements Cloneable {
          private int size;
          private int cap;
          private int[] data;
          private static final int[] EMPTY = new int[0];
  
          public int[] getData() {
              return data;
          }
  
          public IntegerArrayList(int cap) {
              this.cap = cap;
              if (cap == 0) {
                  data = EMPTY;
              } else {
                  data = new int[cap];
              }
          }
  
          public IntegerArrayList(int[] data) {
              this(0);
              addAll(data);
          }
  
          public IntegerArrayList(IntegerArrayList list) {
              this.size = list.size;
              this.cap = list.cap;
              this.data = Arrays.copyOf(list.data, size);
          }
  
          public IntegerArrayList() {
              this(0);
          }
  
          public void ensureSpace(int req) {
              if (req > cap) {
                  while (cap < req) {
                      cap = Math.max(cap + 10, 2 * cap);
                  }
                  data = Arrays.copyOf(data, cap);
              }
          }
  
          public void add(int x) {
              ensureSpace(size + 1);
              data[size++] = x;
          }
  
          public void addAll(int[] x) {
              addAll(x, 0, x.length);
          }
  
          public void addAll(int[] x, int offset, int len) {
              ensureSpace(size + len);
              System.arraycopy(x, offset, data, size, len);
              size += len;
          }
  
          public void addAll(IntegerArrayList list) {
              addAll(list.data, 0, list.size);
          }
  
          public int[] toArray() {
              return Arrays.copyOf(data, size);
          }
  
          public void clear() {
              size = 0;
          }
  
          public String toString() {
              return Arrays.toString(toArray());
          }
  
          public boolean equals(Object obj) {
              if (!(obj instanceof IntegerArrayList)) {
                  return false;
              }
              IntegerArrayList other = (IntegerArrayList) obj;
              return SequenceUtils.equal(data, 0, size - 1, other.data, 0, other.size - 1);
          }
  
          public int hashCode() {
              int h = 1;
              for (int i = 0; i < size; i++) {
                  h = h * 31 + Integer.hashCode(data[i]);
              }
              return h;
          }
  
          public IntegerArrayList clone() {
              IntegerArrayList ans = new IntegerArrayList();
              ans.addAll(this);
              return ans;
          }
  
      }
  
      static class IntegerBIT {
          private int[] data;
          private int n;
  
          public IntegerBIT(int n) {
              this.n = n;
              data = new int[n + 1];
          }
  
          public int query(int i) {
              i = Math.min(i, n);
              int sum = 0;
              for (; i > 0; i -= i & -i) {
                  sum += data[i];
              }
              return sum;
          }
  
          public int query(int l, int r) {
              return query(r) - query(l - 1);
          }
  
          public void update(int i, int mod) {
              if (i <= 0) {
                  return;
              }
              for (; i <= n; i += i & -i) {
                  data[i] += mod;
              }
          }
  
          public String toString() {
              StringBuilder builder = new StringBuilder();
              for (int i = 1; i <= n; i++) {
                  builder.append(query(i) - query(i - 1)).append(' ');
              }
              return builder.toString();
          }
  
      }
  
      static class SequenceUtils {
          public static void swap(int[] data, int i, int j) {
              int tmp = data[i];
              data[i] = data[j];
              data[j] = tmp;
          }
  
          public static boolean equal(int[] a, int al, int ar, int[] b, int bl, int br) {
              if ((ar - al) != (br - bl)) {
                  return false;
              }
              for (int i = al, j = bl; i <= ar; i++, j++) {
                  if (a[i] != b[j]) {
                      return false;
                  }
              }
              return true;
          }
  
      }
  }
  
  