    #include <bits/stdc++.h>
    typedef long long int ll;
    const unsigned int MOD = 1000000007;
     
    using namespace std;
     
    int main()
    {
     
        int t;
        cin >> t;
        for (int tt = 0; tt < t; tt++)
        {
            int n;
            cin>>n;
            ll sum=0;
     
            for (int i = 0; i < n; i++)
            {
                int x;
                cin>>x;
                sum += x;
            }
     
            ll r= sum%n;
     
            cout<<r*(n-r)<<"\n";
     
            
        }
        return 0;
    }