#include <iostream>
#include <cstdlib>
#include <algorithm>

using namespace std;

void solve()
{
    int n;
    cin >> n;
    int a[n];
    long long sum = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        sum += a[i];
    }
    long long x = sum % n;
    long long ans = x * (n - x);
    cout << ans << "\n";
}

int main()
{
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}