import java.io.*;
import java.util.*;

public class Main {

    static long solve(int N, long[] arr) {
        long sum = 0;
        for (int i = 0; i < N; i++) {
            sum += arr[i];
        }
        long na = sum % N;
        return na * (N - na);
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        StringBuilder out = new StringBuilder();
        int T = Integer.parseInt(st.nextToken());
        while (T-- > 0) {
            int N = Integer.parseInt(br.readLine());
            st = new StringTokenizer(br.readLine());
            long[] arr = new long[N];
            for (int i = 0; i < N; i++) {
                arr[i] = Long.parseLong(st.nextToken());
            }

            out.append(solve(N, arr)).append("\n");
        }
        System.out.println(out);
    }
}