#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll solve(vector<ll>&vec,ll n){
	ll sum=0;
	for(auto &i:vec)
		sum+=i;
//	ll eq=sum/n;
	ll rem=sum%n;
	ll ze=n-rem;
	if(rem==0)return 0;
	return ze*rem;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		ll n;
		cin>>n;
		vector<ll>vec(n);
		for(int i=0;i<n;i++)
			cin>>vec[i];
		cout<<solve(vec,n)<<endl;
	}
}