import java.io.*;
import java.util.*; 
public class H {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = null;
        int t = 1;
        
        t = Integer.parseInt(br.readLine());

        while(t-->0) {
            //write code...
            int n = Integer.parseInt(br.readLine());
            st = new StringTokenizer(br.readLine());
            int sum = 0;
            for(int i=0; i<n; i++) {
                sum += Integer.parseInt(st.nextToken());
            }

            int ex = n - sum%n;
            System.out.println(ex*(sum%n));
        }
    }


    static boolean[] isPrime;
    public static void sieve(int n) {
        isPrime = new boolean[n+1];
        Arrays.fill(isPrime, true);
        isPrime[0] = isPrime[1] = false;
        for(int i=2; i*i<=n; i++) {
            if(isPrime[i]) {
                for(int j=i*i; j<=n; j+=i) {
                    isPrime[j] = false;
                }
            }
        }
    }


    public static long pow(long a, long b ) {
        long res = 1;
        while(b>0) {
            if((b&1)==1)
                res *= a;
            a *= a;
            b >>= 1;
        }
        return res;
    }

    public static long gcd(long a, long b) {
        while (b!=0) {
            a %= b;
            long t = a;
            a = b;
            b = t;
        }
        return a;
    }

    public static long lcm(long a, long b) {
        return a*b/gcd(a,b);
    }
}

class Pair {
    int f;
    int s;
    Pair() {}
    Pair(int f, int s) {
        this.f = f;
        this.s = s;
    }
}