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

#define ll long long int
#define pll pair<ll, ll>
#define vl vector<ll>
#define pb push_back
#define pp pop_back
#define f(i, a, b) for (ll i = a; i < b; ++i)
#define rev(i, a, b) for (ll i = a; i >= b; --i)
#define print(x) cout << x << endl
#define endl '\n'
#define F first
#define S second
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define to(n) to_string(n)
#define sort(v) sort(v.begin(), v.end())
#define low(v, n) lower_bound(v.begin(), v.end(), n) - v.begin()
#define upp(v, n) upper_bound(v.begin(), v.end(), n) - v.begin()
#define mod 1000000007
#define mex 100005

ll exp(ll x, ll y)
{
    if (y == 0)
    {
        return 1;
    }
    if (y % 2 == 0)
    {
        return exp((x % mod) * (x % mod), y / 2) % mod;
    }
    else
    {
        return ((x % mod) * exp((x % mod) * (x % mod), y / 2) % mod);
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    // t=1;
    while (t--)
    {
        ll n;
        cin >> n;
        ll p = 0;
        ll A[n];
        f(i, 0, n)
        {
            cin >> A[i];
            p += A[i];
        }

        ll k = p % n;
        //print(k);
        ll a = k * (n - k);
        print(a);
    }

    return 0;
}