#include<bits/stdc++.h>
using namespace std;
#define all(a) a.begin(),a.end()
#define M 1000000007
#define INF LLONG_MAX
typedef long long ll;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
typedef pair<long long,long long> pll;
#define F first
#define S second
#define PB push_back
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(a);i>(b);i--)
#define sz(a) (int)((a).size())
#define CASE cout<<"Case "<<_<<":\n";
int __=1;
void show_(int x){cerr<<x<<'\n';}
void show_(long long x){cerr<<x<<'\n';}
void show_(char x){cerr<<x<<'\n';}
void show_(vector<int> a){cerr<<"[ "; for(auto x:a) cerr<<x<<", "; cerr<<"]\n";}
void show_(vector<long long> a){cerr<<"[ "; for(auto x:a) cerr<<x<<", "; cerr<<"]\n";}
void show_(vector<char> a){cerr<<"[ "; for(auto x:a) cerr<<x<<", "; cerr<<"]\n";}
void fast()
{
    ios_base::sync_with_stdio(0);cin.tie(NULL); cout.tie(NULL);
    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("dbg.txt", "w", stderr);
    #define debug(x) if(true) cerr<<#x<<" = "; show_(x);
    #else
    #define debug(x)
    #endif
}
int main()
{
    fast();
    cin>>__;
    for(int _=1;_<=__;_++)
    {
        int n;
        cin>>n;
        vll a(n);
        for(int i=0;i<n;i++)
        cin>>a[i];
        ll sum = accumulate(all(a),0ll);
        ll extra = sum%n;
        ll normal = n-extra;
        ll ans = normal*extra;
        cout<<ans<<"\n";

    }
    return 0;
}