description: 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.
solution:     import java.io.BufferedReader;
      import java.io.FileInputStream;
      import java.io.FileNotFoundException;
      import java.io.IOException;
      import java.io.InputStreamReader;
      import java.util.*;
  import java.util.concurrent.LinkedBlockingDeque;
  
  import javax.print.attribute.HashAttributeSet;
  import javax.swing.JLabel;
  
  import java.io.PrintStream;
              
                  public class Solution {
  
                      public static boolean Local(){
                          try{ 
                              return System.getenv("LOCAL_SYS")!=null;
                          }catch(Exception e){
                              return false;
                          }
                      }   
  
                      public static boolean LOCAL;
  
                      static class FastScanner {
  
                              BufferedReader br;
                              StringTokenizer st ;
  
                              FastScanner(){
                                  br =  new BufferedReader(new InputStreamReader(System.in));
                                  st = new StringTokenizer("");
                              }
  
                              FastScanner(String file) {
  
                                  try{
                                      br =  new BufferedReader(new InputStreamReader(new FileInputStream(file)));
                                      st = new StringTokenizer("");
                                  }catch(FileNotFoundException e) {
                                      // TODO Auto-generated catch block
                                      System.out.println("file not found");
                                      e.printStackTrace();
                                  }
                                  
                              }
                              
  
                              String next() {
                                  while (!st.hasMoreTokens())
                                      try {
                                          st = new StringTokenizer(br.readLine());
                                      } catch (IOException e) {
                                      }
                                  return st.nextToken();
                              }
  
                              int nextInt() {
                                  return Integer.parseInt(next());
                              }
  
                              long nextLong() {
                                  return Long.parseLong(next());
                              }
                              String readLine() throws IOException{
                                  return br.readLine();
                              }
                          }
                                              
                      static class Pair<T,X> {
  
                          T first;
                          X second;
                          
                          Pair(T first,X second){
                              this.first = first;
                              this.second = second;
                          }
  
                          @Override
                          public int hashCode(){
                              return Objects.hash(first,second);
                          }
  
                          @Override
                          public boolean equals(Object obj){
                              return obj.hashCode() == this.hashCode();
                          }
  
                      }
  
                  
                  static PrintStream debug = null;
                  
                  static class TreeNode{
                      long cnt;
                      int l;
                      int r;
                      TreeNode left;
                      TreeNode right;
                      
                      TreeNode(int l,int r){
                          this.l = l;
                          this.r = r;
                          this.cnt = 0;
                      }
                        
                  }
  
  
                  static long mod = (long)(Math.pow(10,9) + 7); 
                  
  
                  public static void main(String[] args) throws Exception {
                      
                      FastScanner s = new FastScanner();
  
                      LOCAL = Local();
  
                      
                      if(LOCAL){
                          s = new FastScanner("src/input.txt");
                          PrintStream o = new PrintStream("src/sampleout.txt");
                          debug = new PrintStream("src/debug.txt");
                          System.setOut(o);
                      }
  
                      long mod = 1000000007;
  
                  
  
                  
                      int tcr = s.nextInt();                  
                      
                      StringBuilder sb = new StringBuilder();
  
                      for(int tc=0;tc<tcr;tc++){
                          
                          int n = s.nextInt();
  
                          long arr[] = new long[n];
  
                          for(int i=0;i<n;i++){
                              arr[i] = s.nextLong();
                          }
  
                          revalue(arr);
  
                          int max = 0;
  
                          for(int i=0;i<arr.length;i++){
                              max = Math.max((int)arr[i],max);
                          }
  
                          max++;
          
                          TreeNode root = build(0,max);
  
                          long ans = 0l;
  
                          for(int i=0;i<arr.length;i++){
  
                              int num = (int)arr[i];
  
                              ans = ans + Math.min(query(root,0,num-1),query(root,num+1,max)); 
                              //System.out.println(num+" "+ans);
                              update(root,num);
  
                          }
  
                          sb.append(ans+"\n");
  
  
  
                      }
  
                      print(sb.toString());
                  }
  
                  public static void revalue(long arr[]){
                      Map<Long,Integer> map = new HashMap<>();
                      long cpy[] = Arrays.copyOf(arr,arr.length);
                      sort(cpy);
                      int num = 1;
                      for(int i=0;i<cpy.length;i++){
                          if(!map.containsKey(cpy[i])){
                              map.put(cpy[i],num);
                              num++;
                          }
                      }
  
                      for(int i=0;i<arr.length;i++){
                          arr[i] = map.get(arr[i]);
                      }
                  }
  
                  public static TreeNode build(int l,int r){
          
                      if(l == r){
                          return new TreeNode(l,l);
                      }
                      
                      int mid = (l + r)/2;
                      
                      TreeNode curr = new TreeNode(l,r);
                      
                      curr.left  = build(l,mid);
                      curr.right = build(mid+1,r);
                      
                      return curr;
                  }
                  
                  public static void update(TreeNode root,int index){
                      
                      if(root.l == index && root.r == index){
                          root.cnt++;
                          return;
                      }
                      
                      int mid = (root.l + root.r)/2;
                      
                      if(index <= mid){
                          update(root.left,index);
                      }else{
                          update(root.right,index);
                      }
                      
                      root.cnt = root.left.cnt + root.right.cnt;
                      
                  }
                  
                  public static long query(TreeNode root,int l,int r){
                      
                      if(l > r){return 0;}
                      
                      if(root.l == l && root.r == r){
                          return root.cnt;
                      }
                      
                      int mid = (root.l + root.r)/2;
                      
                      int ls = l;
                      int le = Math.min(r,mid);
                      int rs = Math.max(mid+1,l);
                      int re = r;
                      
                      return query(root.left,ls,le) + query(root.right,rs,re);
                      
                  }
                  
  
                  public static List<long[]> prime_factors(long n){
              
                      List<long[]> list = new ArrayList<>();
                      
                      for(long i=2;i<=(Math.sqrt(n));i++){
              
                          if(n % i == 0){
                              
                              int cnt = 0;
                              
                              while( (n % i) == 0){
                                  n = n/i;
                                  cnt++;
                                  
                              }
              
                              list.add(new long[]{i,cnt});
                          }
                      }
  
              
                      if(n!=1){
                          list.add(new long[]{n,1l});
                      }
              
                      return list;
              
                  }
  
                  public static boolean check(int arr[]){
                      for(int i=1;i<arr.length;i++){
                          if(arr[i] < arr[i-1]){
                              return false;
                          }
                      }
                      return true;
                  }
  
              
                  public static boolean inRange(int r1,int r2,int val){
                      return ((val >= r1) && (val <= r2));
                  }
  
                  static int len(long num){
                      return Long.toString(num).length();
                  }
  
                  static long mulmod(long a, long b,long mod)
                  {
                      long ans = 0l;
                      
                      while(b > 0){
                          long curr = (b & 1l);
                          if(curr == 1l){
                              ans = ((ans % mod) + a) % mod;
                          }
                          
                          a = (a + a) % mod;
  
                          b = b >> 1;
                      }
  
                      return ans;
                  }
  
  
                  public static void dbg(PrintStream ps,Object... o) throws Exception{
                      if(ps == null){
                          return;
                      }
                      Debug.dbg(ps,o);
                  }
  
                  public static long modpow(long num,long pow,long mod){
  
                      long val = num;
                      long ans = 1l;
  
                      while(pow > 0l){
                          
                          long bit = pow & 1l;
  
                          if(bit == 1){
                              ans = (ans * (val%mod))%mod;
                          }
  
                          val = (val * val) % mod;
                          pow = pow >> 1;
                      }
  
                      return ans;
  
                  }
  
                  public static char get(int n){
                      return (char)('a' + n);
                  }
  
                  public static long[] sort(long arr[]){
                      List<Long> list = new ArrayList<>();
                      for(long n : arr){list.add(n);}
                      Collections.sort(list);
                      for(int i=0;i<arr.length;i++){
                          arr[i] = list.get(i);
                      }
                      return arr;
                  }
  
                  public static int[] sort(int arr[]){
                      List<Integer> list = new ArrayList<>();
                      for(int n : arr){list.add(n);}
                      Collections.sort(list);
                      for(int i=0;i<arr.length;i++){
                          arr[i] = list.get(i);
                      }
                      return arr;
                  }
  
                  // return the (index + 1)
                  // where index is the pos of just smaller element
                  // i.e count of elemets strictly less than num
                  public static int justSmaller(long arr[],long num){
                      //  System.out.println(num+"@");
                      int st = 0;
                      int e = arr.length - 1;
              
                      int ans = -1;
              
                      while(st <= e){
              
                          int mid = (st + e)/2;
              
                          if(arr[mid] >= num){
                              e = mid - 1;
                          }else{
                              ans = mid;
                              st = mid + 1;
                          }
              
                      }
              
              
                      return ans + 1;
              
                  }
                  public static int justSmaller(int arr[],int num){
                      //  System.out.println(num+"@");
                      int st = 0;
                      int e = arr.length - 1;
              
                      int ans = -1;
              
                      while(st <= e){
              
                          int mid = (st + e)/2;
              
                          if(arr[mid] >= num){
                              e = mid - 1;
                          }else{
                              ans = mid;
                              st = mid + 1;
                          }
              
                      }
              
              
                      return ans + 1;
              
                  }
                  
                  //return (index of just greater element)
                  //count of elements smaller than or equal to num
  
                  public static int justGreater(long arr[],long num){
                      int st = 0;
                      int e = arr.length - 1;
              
                      int ans = arr.length;
              
                      while(st <= e){
                          
                          int mid = (st + e)/2;
              
                          if(arr[mid] <= num){
                              st = mid + 1;
                          }else{
                              ans = mid;
                              e = mid - 1;
                          }
                      }
              
                      return ans;
              
                  }
  
                  public static int justGreater(int arr[],int num){
                      int st = 0;
                      int e = arr.length - 1;
              
                      int ans = arr.length;
              
                      while(st <= e){
                          
                          int mid = (st + e)/2;
              
                          if(arr[mid] <= num){
                              st = mid + 1;
                          }else{
                              ans = mid;
                              e = mid - 1;
                          }
                      }
              
                      return ans;
              
                  }
              
                  
  
                  public static void println(Object obj){
                      System.out.println(obj.toString());
                  }
  
                  public static void print(Object obj){
                      System.out.print(obj.toString());
                  }
  
                  public static int gcd(int a,int b){
                      if(b == 0){return a;}
                      return gcd(b,a%b);
                  }
  
                  public static long gcd(long a,long b){
                      if(b == 0l){
                          return a;
                      }
  
                      return gcd(b,a%b);
                  }
  
                  public static int find(int parent[],int v){
                      if(parent[v] == v){
                          return v;
                      }
                      
                      return parent[v] = find(parent, parent[v]);
                  }
  
              
  
                  public static List<Integer> sieve(){
                  
                          List<Integer> prime = new ArrayList<>();
  
                          int arr[] = new int[100001];
  
                      Arrays.fill(arr,1);
  
                      arr[1] = 0;
                      arr[2] = 1;
  
                      for(int i=2;i<=100000;i++){
                          
                          if(arr[i] == 1){
                              
                              prime.add(i);
  
                              for(long j = (i*1l*i);j<100001;j+=i){
                                  arr[(int)j] = 0;
                              }
                          }
                      }
  
                      return prime;
                  }
  
                  static boolean isPower(long n,long a){
                      
                      long log = (long)(Math.log(n)/Math.log(a));
  
                      long power = (long)Math.pow(a,log);
  
                      if(power == n){return true;}
  
                      return false;
                  }
  
  
                  private static int mergeAndCount(int[] arr, int l,int m, int r)
                  {
  
                      // Left subarray
                      int[] left =  Arrays.copyOfRange(arr, l, m + 1);
  
                      // Right subarray
                      int[] right = Arrays.copyOfRange(arr, m + 1, r + 1);
  
                      int i = 0, j = 0, k = l, swaps = 0;
  
                      while (i < left.length && j < right.length) {
                          if (left[i] <= right[j])
                              arr[k++] = left[i++];
                          else {
                              arr[k++] = right[j++];
                              swaps += (m + 1) - (l + i);
                          }
                      }
                      while (i < left.length)
                          arr[k++] = left[i++];
                      while (j < right.length)
                          arr[k++] = right[j++];
                      return swaps;
                  }
  
                  // Merge sort function
                  private static int mergeSortAndCount(int[] arr, int l,int r)
                  {
  
                      // Keeps track of the inversion count at a
                      // particular node of the recursion tree
                      int count = 0;
  
                      if (l < r) {
                          int m = (l + r) / 2;
  
                          // Total inversion count = left subarray count
                          // + right subarray count + merge count
  
                          // Left subarray count
                          count += mergeSortAndCount(arr, l, m);
  
                          // Right subarray count
                          count += mergeSortAndCount(arr, m + 1, r);
  
                          // Merge count
                          count += mergeAndCount(arr, l, m, r);
                      }
  
                      return count;
                  }
  
                  static class Debug{
                      //change to System.getProperty("ONLINE_JUDGE")==null; for CodeForces
                      public static final boolean LOCAL = System.getProperty("ONLINE_JUDGE")==null;
  
                      
  
                      private static <T> String ts(T t) {
                          if(t==null) {
                              return "null";
                          }
                          try {
                              return ts((Iterable) t);
                          }catch(ClassCastException e) {
                              if(t instanceof int[]) {
                                  String s = Arrays.toString((int[]) t);
                                  return "{"+s.substring(1, s.length()-1)+"}\n";
                              }else if(t instanceof long[]) {
                                  String s = Arrays.toString((long[]) t);
                                  return "{"+s.substring(1, s.length()-1)+"}\n";
                              }else if(t instanceof char[]) {
                                  String s = Arrays.toString((char[]) t);
                                  return "{"+s.substring(1, s.length()-1)+"}\n";
                              }else if(t instanceof double[]) {
                                  String s = Arrays.toString((double[]) t);
                                  return "{"+s.substring(1, s.length()-1)+"}\n";
                              }else if(t instanceof boolean[]) {
                                  String s = Arrays.toString((boolean[]) t);
                                  return "{"+s.substring(1, s.length()-1)+"}\n";
                              }
                              try {
                                  return ts((Object[]) t);
                              }catch(ClassCastException e1) {
                                  return t.toString();
                              }
                          }
                      }
                      private static <T> String ts(T[] arr) {
                          StringBuilder ret = new StringBuilder();
                          ret.append("{");
                          boolean first = true;
                          for(T t: arr) {
                              if(!first) {
                                  ret.append(", ");
                              }
                              first = false;
                              ret.append(ts(t));
                          }
                          ret.append("}");
                          return ret.toString();
                      }
                      private static <T> String ts(Iterable<T> iter) {
                          StringBuilder ret = new StringBuilder();
                          ret.append("{");
                          boolean first = true;
                          for(T t: iter) {
                              if(!first) {
                                  ret.append(", ");
                              }
                              first = false;
                              ret.append(ts(t));
                          }
                          ret.append("}");
                          return ret.toString();
                      }
                      public static void dbg(PrintStream ps,Object... o) throws Exception {
                          if(LOCAL) {
                              System.setErr(ps);
                              System.err.print("Line #"+Thread.currentThread().getStackTrace()[2].getLineNumber()+": [");
                              for(int i = 0; i<o.length; i++) {
                                  if(i!=0) {
                                      System.err.print(", ");
                                  }
                                  System.err.print(ts(o[i]));
                              }
                              System.err.println("]");
                          }
                      }
                  }
  
          }
  
  
          