#include<bits/stdc++.h>
using ll = long long int ;
typedef long double ld;
#define loop(i,a,b) for(ll i = a ; i<b ; i++)
#define N cout<<"\n";
#define vi vector<ll>
#define mii map<ll,ll>
#define vpi vector<pair<ll,ll>>
#define f first
#define s second
#define pb push_back
#define MOD 1000000007
#define MAX 1000001

#define test4(x,y,z,a) cout<<"x is "<<x<<"		y is "<<y<<"		z is "<<z<<"		a is "<<a<<endl;
#define test3(x,y,z) cout<<"x is "<<x<<"		y is "<<y<<"		z is "<<z<<endl;
#define test2(x,y) cout<<"x is "<<x<<"		y is "<<y<<endl;
#define test1(x) cout<<"x is "<<x<<endl;
using namespace std;
/// seive of primes number ////
vector<bool>seive(MAX , true);
vi primes;
void createseive(){
    for (ll i = 2 ; i<MAX ; i++){
        if (seive[i]){
            primes.pb(i);
            for (ll j = i*i ; j<MAX ; j += i){
                seive[j] = false;
            }
        }
    }
}
void printprimes(ll n){
    for (int i = 0 ; i<n ; i++){
        cout<<primes[i]<<" ";
    }
}
/* seive codes end here */

/* power of a number code */
ll power(ll base , ll n){
    ll ans = 1 ;
    while (n!= 0){
        if (n&1){
            ans = (ans*base)%MOD;
            n = n-1;
        }
        else{
            base = (base*base)%MOD;
            n = n >> 1;
        }
    }
    return ans;
}
/*power code ends here */

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int main(){

    ll t;
    cin >> t;
    while (t--){
        ll n;
        cin >> n;
        vector<ll> a(n);
        ll sum = 0;
        for (auto &i : a) {
            cin >> i;
            sum += i;
        }

        ll r = sum % n;
        ll nr = n - r;
        cout << r * nr << '\n';
    }


    return 0;
}