#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define vec vector<ll>
#define vpl vector<pair<ll,ll>>
#define vvl vector<vector<ll>>
#define fio ios_base::sync_with_stdio(false)
#define fo(i,n) for(ll i=0;i<n;i++)
#define forv(i,n,N) for(ll i=n;i<N;i++)
#define ford(i,n,N) for(ll i=n;i>=N;i--)
#define fin cin.tie(NULL)
#define fou cout.tie(NULL)
#define mp make_pair
#define F first
#define S second
#define endl "\n"
#define simsort(v) sort(v.begin(),v.end())
#define simsortd(a) sort(a.begin(),a.end(),greater<int>())
#define mod 1000000007

void solve()
{ ll sum=0;
 ll n;cin>>n;vec a(n);fo(i,n){cin>>a[i];sum+=a[i];}
 simsort(a);
 ll cnt=0;
 ll s=sum/n,m=sum%n;ll i=0;
 fo(i,n){if(m>0)a[i]=1;else{a[i]=0;}m--;}
 fo(i,n){
     cnt+=a[n-1-i]*(n-1-i)-a[n-1-i]*i;
 }
 cout<<abs(cnt-0)<<endl;
} 

int main()
{ 
    fio,fin,fou;
   
   ll t=1;
   cin>>t;
  
   for(ll i=1;i<=t;i++)
   {
       solve();
    
   }
}
     