#include <bits/stdc++.h>
using namespace std;
const int N = 1<<20;
int tests,n,a[N],b[N];
long long count_inversions(int l, int r) {
if(l==r) return 0;
int m=(l+r)>>1,i,j,sz=0;
long long ans=0;
ans+=count_inversions(l,m);
ans+=count_inversions(m+1,r);
i=l;j=m+1;
while(i<=m && j<=r) if(a[j]<a[i]) ans+=m-i+1,b[++sz]=a[j++]; else b[++sz]=a[i++];
while(i<=m) b[++sz]=a[i++];
while(j<=r) b[++sz]=a[j++];
for(i=1;i<=sz;i++) a[l+i-1]=b[i];
return ans;
}
int main() {
int i;
scanf("%d", &tests);
while(tests--) {
scanf("%d", &n);
for(i=1;i<=n;i++) scanf("%d", &a[i]);
printf("%lld\n", count_inversions(1,n));
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE4gPSAxPDwyMDsKCmludCB0ZXN0cyxuLGFbTl0sYltOXTsKCmxvbmcgbG9uZyBjb3VudF9pbnZlcnNpb25zKGludCBsLCBpbnQgcikgewoJaWYobD09cikgcmV0dXJuIDA7CglpbnQgbT0obCtyKT4+MSxpLGosc3o9MDsKCWxvbmcgbG9uZyBhbnM9MDsKCWFucys9Y291bnRfaW52ZXJzaW9ucyhsLG0pOwoJYW5zKz1jb3VudF9pbnZlcnNpb25zKG0rMSxyKTsKCWk9bDtqPW0rMTsKCXdoaWxlKGk8PW0gJiYgajw9cikgaWYoYVtqXTxhW2ldKSBhbnMrPW0taSsxLGJbKytzel09YVtqKytdOyBlbHNlIGJbKytzel09YVtpKytdOwoJd2hpbGUoaTw9bSkgYlsrK3N6XT1hW2krK107Cgl3aGlsZShqPD1yKSBiWysrc3pdPWFbaisrXTsKCWZvcihpPTE7aTw9c3o7aSsrKSBhW2wraS0xXT1iW2ldOwoJcmV0dXJuIGFuczsKfQoKaW50IG1haW4oKSB7CglpbnQgaTsKCXNjYW5mKCIlZCIsICZ0ZXN0cyk7Cgl3aGlsZSh0ZXN0cy0tKSB7CgkJc2NhbmYoIiVkIiwgJm4pOwoJCWZvcihpPTE7aTw9bjtpKyspIHNjYW5mKCIlZCIsICZhW2ldKTsKCQlwcmludGYoIiVsbGRcbiIsIGNvdW50X2ludmVyc2lvbnMoMSxuKSk7Cgl9CglyZXR1cm4gMDsKfQ==