#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 100005;
const int Mod = 1000000007;
int n;
int c[maxn<<2];
struct Node
{
int id,k;
}node[maxn];
bool cmp( Node a,Node b )
{
return a.k < b.k;
}
int lowbit( int x )
{
return x&(-x);
}
void add( int x )
{
while( x <= n )
{
c[x] += 1;
x += lowbit(x);
}
}
int sum( int x )
{
int ans = 0;
while( x >= 1 )
{
ans += c[x];
x -= lowbit(x);
}
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif
int cas;
scanf("%d",&cas);
while( cas -- )
{
LL ans = 0,ld,rd;
scanf("%d",&n);
memset( c,0,sizeof(c) );
for( int i = 1; i <= n; i ++ )
{
scanf("%d",&node[i].k);
node[i].id = i;
}
sort( node+1,node+n+1,cmp );
for( int i = 1; i <= n; i ++ )
{
ld = sum( node[i].id ); //能力小于i且下标下与i的个数
rd = i-1 - ld; //能力小于i但下标大于i的个数
ans += ( ld*( n-node[i].id - rd ) ) + rd*( node[i].id - ld - 1 );
add( node[i].id );
}
printf("%I64d\n",ans);
}
return 0;
}