

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;

/**
 *
 * @author is2ac
 */
public class B_CF {

    public static void main(String[] args) {
        FastScanner56 fs = new FastScanner56();
        PrintWriter pw = new PrintWriter(System.out);
        int t = fs.ni();
        //int t = 1;
        for (int tc = 0; tc < t; tc++) {
            int n = fs.ni();
            long[] a = fs.longArray(n);
            long s = 0L;
            for (int i = 0; i < n; i++) {
                s += a[i];
            }
            long av = s/n;
            long[] b = new long[n];
            for (int i = n-1; i > -1; i--) {
                av = s/(long)(i+1);
                b[i] = av;
                s -= av;
            }
            long one = 0, two = 0;
            for (int i = 0; i < b.length; i++) {
                if (b[i]==b[0]) {
                    one++;
                } else {
                    two++;
                }
            }
            pw.println(one*two);
        }
        pw.close();
    }

    public static int f(long n, long ind, long l, long r) {
        while (l < r && n > 1) {
            long m = (r + l) / 2;
            if (m == ind) {
                break;
            }
            if (ind < m) {
                r = m - 1;
            } else {
                l = m + 1;
            }
            n /= 2;
        }
        return (int) (n % 2);
    }

    public static List<Integer> primeFactors(int number) {
        int n = number;
        List<Integer> factors = new ArrayList<Integer>();
        for (int i = 2; i <= n; i++) {
            while (n % i == 0) {
                factors.add(i);
                n /= i;
            }
        }
        return factors;
    }

    // 0 = alice, 1 = bob;
    public static int gcd(int n1, int n2) {
        if (n2 == 0) {
            return n1;
        }
        return gcd(n2, n1 % n2);
    }

    static boolean isp(int x) {
        int y = (int) Math.sqrt(x);
        if (Math.pow(y, 2) == x) {
            return true;
        }
        return false;
    }
}

class FastScanner56 {

    BufferedReader br;
    StringTokenizer st;

    public FastScanner56() {
        br = new BufferedReader(new InputStreamReader(System.in), 32768);
        st = null;
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int ni() {
        return Integer.parseInt(next());
    }

    int[] intArray(int N) {
        int[] ret = new int[N];
        for (int i = 0; i < N; i++) {
            ret[i] = ni();
        }
        return ret;
    }

    long nl() {
        return Long.parseLong(next());
    }

    long[] longArray(int N) {
        long[] ret = new long[N];
        for (int i = 0; i < N; i++) {
            ret[i] = nl();
        }
        return ret;
    }

    double nd() {
        return Double.parseDouble(next());
    }

    String nextLine() {
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }
}
