#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
long long power(long long x, long long y, long long M) {
  long long res = 1;
  while (y > 0) {
    if (y & 1) res = (res * x) % M;
    y >>= 1;
    x = (x * x) % M;
  }
  return res % M;
}
long long power(long long x, long long y) {
  long long res = 1;
  while (y > 0) {
    if (y & 1) res = (res * x);
    y >>= 1;
    x = (x * x);
  }
  return res;
}
const long long mxn = 1e6 + 6, mod = 1e9 + 7, inf = 1e18;
long long t, casee = 1;
template <typename T>
class Segment_Tree {
 private:
  vector<T> arr;
  vector<T> seg_tree;
  long long N;
  long long def;

 public:
  Segment_Tree(vector<T> a, long long deff = 0) {
    N = a.size();
    def = deff;
    arr = a;
    seg_tree.resize(4 * N + 10, 0);
    build();
  }
  long long func(long long a, long long b) { return a + b; }
  void build() { build_it_for_me(0, N - 1, 1); }
  void build_it_for_me(long long lo, long long hi, long long pos) {
    assert(lo <= hi);
    if (lo == hi) {
      seg_tree[pos] = arr[lo];
      return;
    }
    long long mid = (lo + hi) / 2;
    build_it_for_me(lo, mid, 2 * pos);
    build_it_for_me(mid + 1, hi, 2 * pos + 1);
    seg_tree[pos] = func(seg_tree[2 * pos], seg_tree[2 * pos + 1]);
  }
  void update(long long ind, long long val) {
    update_it_for_me(ind, val, 0, N - 1, 1);
  }
  void update_it_for_me(long long ind, long long val, long long lo,
                        long long hi, long long pos) {
    if (lo == hi) {
      seg_tree[pos] = val;
      arr[ind] = val;
      return;
    }
    long long mid = (lo + hi) / 2;
    if (mid >= ind)
      update_it_for_me(ind, val, lo, mid, 2 * pos);
    else
      update_it_for_me(ind, val, mid + 1, hi, 2 * pos + 1);
    seg_tree[pos] = func(seg_tree[2 * pos], seg_tree[2 * pos + 1]);
  }
  T query(long long l, long long r) {
    return query_it_for_me(l, r, 1, 0, N - 1);
  }
  T query_it_for_me(long long l, long long r, long long pos, long long lo,
                    long long hi) {
    if (lo > r || hi < l) return def;
    if (lo >= l && hi <= r) return seg_tree[pos];
    long long mid = (lo + hi) / 2;
    return func(query_it_for_me(l, r, 2 * pos, lo, mid),
                query_it_for_me(l, r, 2 * pos + 1, mid + 1, hi));
  }
};
void test_case() {
  long long n;
  cin >> n;
  vector<long long> a(n), b(n);
  for (long long i = 0; i < n; i++) cin >> a[i], b[i] = a[i];
  map<long long, long long> mapped;
  sort((b).begin(), (b).end());
  long long curr = 0;
  for (long long i = 0; i < n; i++) {
    if (mapped.find(b[i]) == mapped.end()) {
      mapped[b[i]] = curr++;
    }
  }
  for (long long i = 0; i < n; i++) a[i] = mapped[a[i]];
  vector<long long> temp(curr + 10, 0);
  Segment_Tree<long long> bit(temp);
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    long long left = bit.query(0, a[i] - 1);
    long long right = bit.query(a[i] + 1, curr + 5);
    ans += min(left, right);
    bit.update(a[i], bit.query(a[i], a[i]) + 1);
  }
  cout << ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  t = 1;
  cin >> t;
  while (t--) {
    test_case();
    casee++;
  }
  return 0;
}

### Prompt

Please provide a CPP coded solution to the problem described below:

In fact, the problems E1 and E2 do not have much in common. You should probably think of them as two separate problems.

You are given an integer array a[1 … n] = [a_1, a_2, …, a_n].

Let us consider an empty [deque](https://tinyurl.com/pfeucbux) (double-ended queue). A deque is a data structure that supports adding elements to both the beginning and the end. So, if there are elements [3, 4, 4] currently in the deque, adding an element 1 to the beginning will produce the sequence [\color{red}{1}, 3, 4, 4], and adding the same element to the end will produce [3, 4, 4, \color{red}{1}].

The elements of the array are sequentially added to the initially empty deque, starting with a_1 and finishing with a_n. Before adding each element to the deque, you may choose whether to add it to the beginning or to the end.

For example, if we consider an array a = [3, 7, 5, 5], one of the possible sequences of actions looks like this:    1. |  add 3 to the beginning of the deque: |  deque has a sequence [\color{red}{3}] in it;   
---|---|---  
  2. |  add 7 to the end of the deque: |  deque has a sequence [3, \color{red}{7}] in it;   
  3. |  add 5 to the end of the deque: |  deque has a sequence [3, 7, \color{red}{5}] in it;   
  4. |  add 5 to the beginning of the deque: |  deque has a sequence [\color{red}{5}, 3, 7, 5] in it;   
  
Find the minimal possible number of inversions in the deque after the whole array is processed. 

An inversion in sequence d is a pair of indices (i, j) such that i < j and d_i > d_j. For example, the array d = [5, 3, 7, 5] has exactly two inversions — (1, 2) and (3, 4), since d_1 = 5 > 3 = d_2 and d_3 = 7 > 5 = d_4.

Input

The first line contains an integer t (1 ≤ t ≤ 1000) — the number of test cases.

The next 2t lines contain descriptions of the test cases. 

The first line of each test case description contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5) — array size. The second line of the description contains n space-separated integers a_i (-10^9 ≤ a_i ≤ 10^9) — elements of the array.

It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.

Output

Print t lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the minimal possible number of inversions in the deque after executing the described algorithm.

Example

Input


6
4
3 7 5 5
3
3 2 1
3
3 1 2
4
-1 2 2 -1
4
4 5 1 3
5
1 3 1 3 2


Output


2
0
1
0
1
2

Note

One of the ways to get the sequence [5, 3, 7, 5] in the deque, containing only two inversions, from the initial array [3, 7, 5, 5] (the first sample test case) is described in the problem statement. 

Also, in this example, you could get the answer of two inversions by simply putting each element of the original array at the end of the deque. In this case, the original sequence [3, 7, 5, 5], also containing exactly two inversions, will be in the deque as-is.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
long long power(long long x, long long y, long long M) {
  long long res = 1;
  while (y > 0) {
    if (y & 1) res = (res * x) % M;
    y >>= 1;
    x = (x * x) % M;
  }
  return res % M;
}
long long power(long long x, long long y) {
  long long res = 1;
  while (y > 0) {
    if (y & 1) res = (res * x);
    y >>= 1;
    x = (x * x);
  }
  return res;
}
const long long mxn = 1e6 + 6, mod = 1e9 + 7, inf = 1e18;
long long t, casee = 1;
template <typename T>
class Segment_Tree {
 private:
  vector<T> arr;
  vector<T> seg_tree;
  long long N;
  long long def;

 public:
  Segment_Tree(vector<T> a, long long deff = 0) {
    N = a.size();
    def = deff;
    arr = a;
    seg_tree.resize(4 * N + 10, 0);
    build();
  }
  long long func(long long a, long long b) { return a + b; }
  void build() { build_it_for_me(0, N - 1, 1); }
  void build_it_for_me(long long lo, long long hi, long long pos) {
    assert(lo <= hi);
    if (lo == hi) {
      seg_tree[pos] = arr[lo];
      return;
    }
    long long mid = (lo + hi) / 2;
    build_it_for_me(lo, mid, 2 * pos);
    build_it_for_me(mid + 1, hi, 2 * pos + 1);
    seg_tree[pos] = func(seg_tree[2 * pos], seg_tree[2 * pos + 1]);
  }
  void update(long long ind, long long val) {
    update_it_for_me(ind, val, 0, N - 1, 1);
  }
  void update_it_for_me(long long ind, long long val, long long lo,
                        long long hi, long long pos) {
    if (lo == hi) {
      seg_tree[pos] = val;
      arr[ind] = val;
      return;
    }
    long long mid = (lo + hi) / 2;
    if (mid >= ind)
      update_it_for_me(ind, val, lo, mid, 2 * pos);
    else
      update_it_for_me(ind, val, mid + 1, hi, 2 * pos + 1);
    seg_tree[pos] = func(seg_tree[2 * pos], seg_tree[2 * pos + 1]);
  }
  T query(long long l, long long r) {
    return query_it_for_me(l, r, 1, 0, N - 1);
  }
  T query_it_for_me(long long l, long long r, long long pos, long long lo,
                    long long hi) {
    if (lo > r || hi < l) return def;
    if (lo >= l && hi <= r) return seg_tree[pos];
    long long mid = (lo + hi) / 2;
    return func(query_it_for_me(l, r, 2 * pos, lo, mid),
                query_it_for_me(l, r, 2 * pos + 1, mid + 1, hi));
  }
};
void test_case() {
  long long n;
  cin >> n;
  vector<long long> a(n), b(n);
  for (long long i = 0; i < n; i++) cin >> a[i], b[i] = a[i];
  map<long long, long long> mapped;
  sort((b).begin(), (b).end());
  long long curr = 0;
  for (long long i = 0; i < n; i++) {
    if (mapped.find(b[i]) == mapped.end()) {
      mapped[b[i]] = curr++;
    }
  }
  for (long long i = 0; i < n; i++) a[i] = mapped[a[i]];
  vector<long long> temp(curr + 10, 0);
  Segment_Tree<long long> bit(temp);
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    long long left = bit.query(0, a[i] - 1);
    long long right = bit.query(a[i] + 1, curr + 5);
    ans += min(left, right);
    bit.update(a[i], bit.query(a[i], a[i]) + 1);
  }
  cout << ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  t = 1;
  cin >> t;
  while (t--) {
    test_case();
    casee++;
  }
  return 0;
}

```