#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,n) for(int i=0;i<n;i++)
#define endl ("\n")
#define pb push_back
#define mp make_pair
#define pl pair<ll, ll>
#define vl vector<ll>
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);

#define M 1000000007
 const int N=2e5+2;
 ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}
ll mminvprime(ll a, ll b) {return expo(a, b - 2, b);}
vector<ll> sieve(int n) {int*arr = new int[n + 1](); vector<ll> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}
ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
ll mod_div(ll a, ll b, ll m) {a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m;}  //only for prime m

int fact[N];

        void precalc(){
            fact[0]=1;
            for(int i=1;i<N;i++){
                fact[i]=mod_mul(fact[i-1],i,M);
            }
        }
 long long mod(long long x){
            return ((x%M + M)%M);
        }
        ll inv(ll x){
            return expo(x,M-2,M);
        }

        ll divide(ll a, ll b){
            return mod_mul(a,inv(b),M);
        }

        ll nCr(ll n, ll r){
            return divide(fact[n],mod_mul(fact[r],fact[n-r],M));
        }

	
int main()
{
    fastIO
  ll t;
  cin>>t;
  while(t--){
 ll n; cin >> n;
        ll arr[n], sum = 0;
        for(auto &i : arr) cin >> i, sum += i;
 
        ll cnt = sum - (sum / n * n);
        cout << cnt * (n - cnt) << endl;
        

  }




}
