#include <bits/stdc++.h>
#define ll long long
using namespace std;
 
 
int main()
{
	int n;
	cin >> n;
	while(n --)
	{
		ll t;
		cin >> t;
		ll num = 0, s;
		for(int i = 0; i < t; i ++)
		{
			cin >>s;
			num += s;
		}
		
			ll tmp = num - num/t*t;
			cout << tmp*(t-tmp) <<endl;
		
		
	}//tmp=3 t=5 s=1
	return 0;
} 