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

int main()
{
   ll t;
   cin>>t;
   while(t--){
       ll n,total=0;
       cin>>n;
       vector<ll> a(n);
       for(ll i=0;i<n;i++){
           cin>>a[i];
           total+=a[i];
       }
       ll extra=total%n;
       ll withoutextra=n-extra;
       cout<<extra*withoutextra<<endl;
   }
   
   return 0;
}