#include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define endl "\n"
#define ll long long
#define all(x)                      x.begin(),x.end()
#define allr(x)                     x.rbegin(),x.rend()
#define sortAll(x)                  sort(all(x))   
#define clearAll(arr)               memset(arr,0,sizeof(arr))
#define deb(x)                      cout<<#x<<" = "<<x<<endl;
#define PB                          push_back
#define MP                          make_pair
#define PI                          3.1415926535897932384626
typedef pair<long long, long long>  pll;
typedef vector<long long>           vll;
typedef vector<pll>                 vpll;
#define kedar ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll mod=1000000007;
ll mod_chef = 998244353;
ll row[] = {1,-1,0,0};
ll col[] = {0,0,1,-1};
ll dx[] = { -2, -1, 1, 2, -2, -1, 1, 2 }; 
ll dy[] = { -1, -2, -2, -1, 1, 2, 2, 1 }; 
ll rr[] = {1,-1,0,0,-1,1,1,-1};
ll cc[] = {0,0,1,-1,-1,1,-1,1};

//priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll , ll>>> p;

ll ncr(ll n,ll r){ ll res=1; if(r>n-r)r=n-r; for(int i=0;i<r;i++) {  res*=n-i;  res/=i+1; } return res; }
ll power(ll a,ll b,ll m=mod){ ll ans=1; a=a%m;  while(b>0) {  if(b&1)ans=(1ll*a*ans)%m;b>>=1;a=(1ll*a*a)%m;}return ans;}
ll lcm(ll a,ll b) {	return ((a*b)/__gcd(a,b));}
bool revsort(ll a, ll b) {return a > b;}

const long long K = 1e5+5;
const long long N = 1e5+5;
bool prime[N];

void primeFun(){
	memset(prime, true, sizeof(prime));
 
    for (int p = 2; p * p <N; p++)
    {
        if (prime[p] == true) 
        {
            for (int i = p * p; i <N; i += p)
                prime[i] = false;
        }
    }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <<
//  <<,

void solve()
{
	ll n;
	cin>>n;
	vll arr(n);
	for (int i = 0; i < n; ++i) cin>>arr[i];

	ll sum = 0;
	for (int i = 0; i < n; ++i)
	{
		sum += arr[i];
	}
	ll add = sum/n;
	if(add==0){
		
		cout<<(sum*(n-sum))<<endl;

		return;
	}
	int i=0;
	for (;sum>=add && i < n; ++i)
	{
		arr[i] = add;
		sum -= add;
	}
	while(n>i){
		arr[i] = 0;
	}

	if(sum>0){
		int i = 0;
		while(sum>0 && n>i){
			arr[i]++;
			sum--;
			i++; 
		}
	}

	ll inq = 0;
	ll mul = 1;
	for (int i = 0; i < n-1; ++i)
	{
		if(arr[i]!=arr[i+1]){
			mul = i+1;
			break;
		}
	}
	cout<<(mul*(n-mul))<<endl;	
}

int main(){
	kedar;
//  <<
//  << 	
//  ,
	#ifndef ONLINE_JUDGE
	time_t start, end;
    time(&start);
    ios_base::sync_with_stdio(false);
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif

	// primeFun();
	
	ll t=1;
	cin>>t;
	while(t--) solve();

	#ifndef ONLINE_JUDGE
    time(&end);
    double time_taken = double(end - start);
    cout <<endl<< "Time taken by program is : " << fixed
         << time_taken << setprecision(5);
    cout << " sec " << endl;
    #endif
	return 0;
}