#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;

int ans[200005];
int a[200005];

int main(){
    ll t;
    cin>>t;
    while(t--){
        memset(a,0,sizeof(a));
        ll n;
        cin>>n;
        ll sum=0;
        for(int i=1;i<=n;i++){
            cin>>ans[i];
            sum+=ans[i];
        }
        //sort(ans+1,ans+n+1);
        //int pin=sum/n;
        ll yu=sum%n;
        ll inc=0;
        /*for(int i=1;i<=yu;i++){
            a[i]=1;
        }
        
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++){
                inc+=abs(a[i]-a[j]);
            }
        }*/
        
        inc=yu*(n-yu);
        cout<<inc<<endl;
    }
    return 0;
}