#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 1000100
int num[N];
long long ans;
void guibing(int a_1,int a_2,int b_1,int b_2){//归并函数
int n_1,t,m_1;
int* temp=new int[b_2-a_1+5];
n_1=a_1;
m_1=b_1;
t=0;
while(n_1<=a_2&&m_1<=b_2){//归并函数的中心思想
if(num[n_1]<=num[m_1]){
temp[t++]=num[n_1++];
continue;
}
else{
temp[t++]=num[m_1++];
ans+=(a_2-n_1+1);
continue;
}
}
while(n_1<=a_2)
temp[t++]=num[n_1++];
while(m_1<=b_2)
temp[t++]=num[m_1++];
for(int i=a_1;i<=b_2;i++)
num[i]=temp[i-a_1]; //这个地方不是太了解
delete temp;
// return ans;
}
void deal(int first,int last){
int mod;
if(first<last){
mod=(first+last)/2;
deal(first,mod);
deal(mod+1,last);
guibing(first,mod,mod+1,last);
}
}
int main(){
int i,j,k,t,n,m;
int T;
scanf("%d",&T);
while(T--){
ans=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&num[i]);
deal(0,n-1);
printf("%lld\n",ans);
}
return 0;
}