
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MOD = 1000000007;

//-----------INLINE FUNC---------
inline int gcd(int a, int b) {return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b) {return a * b / gcd(a, b); }
inline int add(int a, int b) {return ((a % MOD) + (b % MOD)) % MOD;}
inline int multi(int a, int b) {return ((a % MOD) * (b % MOD)) % MOD;}
inline int sub(int a, int b) 
{ a %= MOD; b %= MOD; a -= b; if (a < 0) a += MOD; return a;}

bool isPrime(int  x)
{   
	if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3; 
	for (int i = 5; i * i <= x; i += 6)
	{
		if (x % i == 0 || x % (i + 2) == 0) { return 0;}
	}
	return 1;
}
 
ll prime[1000002];
 
int Prime(int a)// FOR SINGLE TO KNOW FOR PRIME
{
    for(int i=2;i<=a/2;++i)
    {
        if(a%i==0)
        {
            return 0;
        }
    }
    return 1;
}
 
void sieve()// FOR PRIMES TILL A VALUE
{   for(int i=0;i<=1000001;i++)
    prime[i]=0;
    int maxn=1000001;
    for(int i=2;i<=maxn;i++)
    {
        if(prime[i]==0)
        {
            for(int j=i;j<=maxn;j+=i)
            {
                prime[j]++;
            }
        }
    }
}
 int ceilIndex(vector<int> a, int l, int h, int find)
    {
        
        while(l<h)
        {
            int m=(l+h)/2;
            if(a[m]>=find) h=m;
            else l=m+1;
        }
        return h;        
    }
   
    
    void run_fun()
    {
       long long  int n;cin>>n;
       long long int s=0,in;
        for(long long int i=0;i<n;i++)
        {
         cin>>in;
         s+=in;
        }
        long long int fir=s%n;
        long long int sec=(n-s%n);
        cout<<fir*(sec)<<endl;
     
    }
    int main() 
    {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int t; cin>>t;
    while(t--) run_fun();
    return 0;
    
    }
