#include<bits/stdc++.h>
#define ll long long
using namespace std;

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

   ll t;
   cin >> t;
   while(t--)
   {
        ll i,n,x,s=0,ans;
        cin >> n;
        for(i=0;i<n;i++)
        {
            cin >> x;
            s+=x;
        }
        if(s%n==0)
            ans=0;
        else
        {
            ll y=s%n;
            ans=(n-y)*y;
        }
        cout << ans << "\n";
   }
   return 0;
}
