#include<bits/stdc++.h>
#include<time.h>
#include<string>
#include<math.h>
#include<cmath>
#include<algorithm>

using namespace std;
#define ll long long int

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        ll sum = 0;
        int a[n];
        for(int i = 0; i<n; i++){
            cin>>a[i];
            sum += a[i];
        }
        ll r = sum%n;
        cout<<(n-r)*r<<endl;
    }
    return 0;
}