// 離散化只能求沒有重復的一組數的逆序對數
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 1000005
struct node
{long long val,order;
}num[N]; // 存儲輸入的原數組
long long a[N]; // 存儲離散后的數據
long long c[N]; // 存樹狀數組
long long n;
long long lowbit(long long x)
{return x&(-x);
}
void update(long long x,long long t)
{while(x<=n){c[x]+=t;x+=lowbit(x);}
}
long long getsum(long long x)
{long long temp=0;while(x>=1){temp+=c[x];x-=lowbit(x);}return temp;
}
int cmp(const void *a,const void *b)
{return (((struct node *)a)->val-((struct node *)b)->val);
}
int main()
{long long T,i,ans,count;scanf("%lld",&T);while(T--){scanf("%lld",&n);for(i=1;i<=n;i++) // 輸入的原數組{scanf("%lld",&num[i].val);num[i].order=i;}qsort(num+1,n,sizeof(num[1]),cmp);// 離散化count=0;num[i-1].val=0;for(i=1;i<=n;i++){if(num[i].val==num[i-1].val) // 去重a[num[i].order]=count;elsea[num[i].order]=++count;}// 用樹狀數組求逆序數memset(c,0,sizeof(c));ans=0;for(i=1;i<=n;i++){update(a[i],1);ans+=i-getsum(a[i]);}printf("%lld\n",ans);}return 0;
}