#include<bits/stdc++.h>
#define ll long long int
#define mk make_pair
#define pb push_back

#define pii pair<ll,ll>
#define mod 998244353
#define ff first
#define ss second
#define srt(v)  if(!v.empty())sort(v.begin(),v.end())
#define rev(v)  if(!v.empty())reverse(v.begin(),v.end())
#define PI 3.141592653589793238
#define pqr priority_queue<ll,vector<ll>,greater<ll>()>
using namespace std;
ll pow_mod(ll a,ll b)
{
	ll res=1;
	while(b!=0)
	{
		if(b&1)
		{
			res=(res*a)%mod;
		}
		a=(a*a)%mod;
		b/=2;
	}
	return res;
}

ll inv_mod(ll x){
    return pow_mod(x,mod-2);
}

const ll N=(ll)3e5;
ll inv[N];
ll fac[N];

void init(){
  fac[0]=1;
  
  for(ll i=1;i<N;i++)
  fac[i]=(i*fac[i-1])%mod;
  
  for(ll i=0;i<N;i++)
  inv[i]=inv_mod(fac[i]);
}

ll ncr(ll n,ll r){
    if(n<r)
    return 0;
    
    if(n==r||r==0)
    return 1LL;
    
    ll x=fac[n];
    ll y=inv[n-r];
    ll z=inv[r];
    
    return ((x*y)%mod*z)%mod;
}
bool test_cases=1;
void solve()
{
    ll n;
    cin>>n;
    
    ll a[n+1];
    
    for(ll i=0;i<n;i++)
    cin>>a[i];
    
    ll ans=0;
    
    ll tot=0;
    
    for(ll i=0;i<n;i++)
    tot+=a[i];
    
    ll each=tot/n;
    
    ll rem=tot%n;
    
    for(ll i=0;i<n;i++)
    a[i]=each;
    
    for(ll i=0;i<rem;i++)
    a[i]++;
    
    
    sort(a,a+n);
    
    
    
    for(ll i=0;i<n;i++){
       // cout<<a[i]<<" ";
        ans-=(n-i-1)*a[i];
        ans+=i*a[i];
    }
    
    cout<<ans<<endl;
    
    
}
int main()
{   
     mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 
     ios_base::sync_with_stdio(false);
     cin.tie(NULL);
     cout.tie(NULL);
   
   // init();   //factorial calculations
   
   //Start from Here.

  ll t;
  t=1;
  
  if(test_cases)
  cin>>t;
  
  while(t--)
  solve();
//Good Bye!
    return 0;
}