#include<bits/stdc++.h>
using namespace std;

#define int long long
#define fl(i,n) for(int i=0;i<n;i++) 
#define v(i) vector<int>
#define um unordered_map<int,int> 
#define pb push_back
#define pr pair<int,int>
#define NO cout<<"NO";
#define all(a) a.begin(),a.end()
#define YES cout<<"YES"; 
#define sortn(a) sort(a.begin(),a.end())
#define sortc(a,cp) sort(a.begin(),a.end(),cp)
#define pq priority_queue<int> 
#define vvi vector<v(i)>
#define reverse(a) reverse(a.begin(),a.end()) 

/* lcm(a,b) , _gcd(a,b) , swap(a,b) , sort(a.begin(),a.end()) , *max_element(a.begin(),a.end()) , *min_element(a.begin(),a.end()) */
/* _builtin_popcount(x): This function is used to count the number of one’s */
/*__builtin_clz(x): This function is used to count the leading zeros of the integer. */
/* string s.find("ks")==string::npos if not found */
/*__builtin_ctz(x): This function is used to count the trailing zeros of the given integer */
/* cout<<fixed<<setprecision(8)   ,  round(1.589) -> 2 , trunc(1.590) -> 1 , ceil(x) , floor(x) */


int power(int x, int y, int m) 
{ 
    if (y == 0) 
        return 1; 
    int p = power(x, y / 2, m) % m; 
    p = (p * p) % m; 
  
    return (y % 2 == 0) ? p : (x * p) % m; 
}   

/* inverse of y is power(y,p-2,p) */



void print(v(i) a){
fl(i,a.size())
cout<<a[i]<<" ";
cout<<"\n";
}
int gcd( int a, int b)
{
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
 

int lcm(int a, int b)
{
    return (a / gcd(a, b)) * b;
}

bool cmp(int a,int b){ return a>b;}

int32_t main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int t;
    cin>>t;
    fl(tc,t){
        int n; cin>>n;
        v(i) a(n);
        fl(i,n) cin>>a[i];
        sortn(a);
        int i=0,j=n-1;
       int s=0;
       fl(i,n) s+=a[i];
       int k=s/n;
       int rm=s%n;
       fl(i,n) a[i]=k;
       fl(i,rm) a[i]++;
        int ans=0,pf=0;
        sortn(a);
        fl(i,n){
            int c=i*a[i]-(pf);
            ans+=c;
            pf+=a[i];
        }
    cout<<ans<<"\n";    
    }
}
