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

#define ll long long
#define pb push_back
#define pp pop_back
#define endl '\n'
#define mp make_pair
#define pi (3.141592653589)
#define ff first
#define ss second
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define vpii vector <pair <int, int> >
#define pii pair<int,int>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

void solve(){
    int n;
    cin >> n;
    ll ans=0;
    rep(i,n){
        int j;
        cin>>j;
        ans+=j;
    }
    if(ans<n){
        cout<<ans*(n-ans);
    }else{
        cout<<(n-(ans%n))*(ans%n);
    }
}
int main(){
  int t;
  cin >> t;
  while(t--){
        solve();
        cout<<endl;
    }
}