import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while (t-- > 0) {
int n = scanner.nextInt();
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = scanner.nextInt();
int s = 0, e = n - 1;
long count = mergeSort(arr, s, e);
System.out.println(count);
}
scanner.close();
}
private static long mergeSort(int[] arr, int s, int e) {
long count = 0;
if (s < e) {
int mid = (s + e)/2;
count += mergeSort(arr, s, mid);
count += mergeSort(arr, mid + 1, e);
count += merge(arr, s, mid, e);
}
return count;
}
private static long merge(int[] arr, int s, int mid, int e) {
long count = 0;
int temp1[] = Arrays.copyOfRange(arr, s, mid + 1),
temp2[] = Arrays.copyOfRange(arr, mid + 1, e + 1);
int k = s, i = 0, j = 0;
while (i < temp1.length && j < temp2.length) {
if (temp1[i] <= temp2[j]) {
arr[k++] = temp1[i++];
} else {
arr[k++] = temp2[j++];
count += (mid + 1) - (s + i);
}
}
while (i < temp1.length)
arr[k++] = temp1[i++];
while (j < temp2.length)
arr[k++] = temp2[j++];
return count;
}
}