#include <iostream>
#include <string>
#include <map>
#include <list>
#include <iterator>
#include <cmath>
#include <tuple>
#include <vector>
#include <algorithm>
#include <chrono>
#include <set>
#include <functional>
#include <utility>
#include <bitset>
#include <climits>
#include <numeric>

#define ll long long
#define ull unsigned long long int    
#define endl '\n'
#define PB push_back
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
#define vvl vector<vl>

double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
ll M = 10e8 + 7;

using namespace std;


void solve()
{
	ll n; cin >> n;
	ll a, sum = 0;

	for (int i = 0; i < n; i++)
	{
		cin >> a;
		sum += a;
	}

	ll d1 = sum / n;
	ll d2 = sum / n;
	if (sum%n != 0)d2++;
	ll a1, a2;
	if (d1 == d2) 
	{
		cout << 0 << endl;
		return;
	}
	else
	{
		a1 = sum - n * d1;
		a2 = n * d2 - sum;
		if (a1 > a2) a1 = a2;
		
	}
	cout << (n - a1)*a1 << endl;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	ll t; cin >> t; while (t--)
	solve();

}