#include<bits/stdc++.h>
using namespace std ;
long long int temp[200000] ;
long long int a[200000] ;
long long inversioncount( int s , int e ){
// Handling the base case of the recursive function
if(s==e) return 0 ; // one element no need to sort and no inversion
if(s==e-1){
// two elements either one inversion or no inversion
if(a[s] <= a[e] ) return 0 ;
else{
swap(a[s] , a[e] );
return 1 ;
}
}
int mid = (s+e)/2 ;
long long ans = 0 ;
// adding contribution to inversion from right and left subarray
ans += inversioncount( s , mid ) ;
ans += inversioncount( mid+1 , e ) ;
int l = s , r = mid+1 , k = s;
// merging and caluclating remaining inversions
while(l<=mid and r<=e){
if(a[l]<=a[r]){
// no inversion simply add the element
temp[k] = a[l] ;
l ++ ; k ++ ;
}else{
// inversions! add element to temp array and reflect inversions in final answer
temp[k] = a[r] ;
r ++ ; k++ ;
ans += mid - l + 1 ;
}
}
//Handle left over elements from the two arrays as in case of merge sort
while(l<=mid){
temp[k] = a[l] ;
l ++ ; k ++ ;
}
while(r<=e){
temp[k] = a[r] ;
r ++ ; k++ ;
}
// copy sorted elements back from the sorted temp array to the original array
for(int i = s ; i <= e ; i ++ ){
a[i] = temp[i] ;
}
return ans ;
}
void solve(){
//getchar() ;
int n ;
cin >> n ;
for( int i = 0 ; i < n ; i ++){
cin >> a[i] ;
}
cout << inversioncount( 0 , n-1) << "\n" ;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
long long tc ;
cin >> tc ;
while(tc--){
solve() ;
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZCA7CmxvbmcgbG9uZyBpbnQgdGVtcFsyMDAwMDBdIDsgCmxvbmcgbG9uZyBpbnQgYVsyMDAwMDBdIDsgCmxvbmcgbG9uZyBpbnZlcnNpb25jb3VudCggaW50IHMgLCBpbnQgZSApewogICAgLy8gSGFuZGxpbmcgdGhlIGJhc2UgY2FzZSBvZiB0aGUgcmVjdXJzaXZlIGZ1bm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