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

public class Codeforces {

    public static void main(String[] args) throws IOException {
        FastScanner fs = new FastScanner();
        PrintWriter pw = new PrintWriter(System.out);
        int t = 1;
        t = fs.getInt();
        while (t-- > 0) {
            int n = fs.getInt();

            long[] ar = new long[n];

            long sum = 0;
            for (int i = 0; i < n; i++) {
                ar[i] = fs.getInt();
                sum += ar[i];
            }

            long a = sum / n;
            long b = sum % n;

            for (int i = 0; i < ar.length; i++) {
                ar[i] = a;
            }

            int j = 0;
            while (b != 0) {
                ar[j % n] += 1;
                b -= 1;
                j += 1;
            }

            Utility.sort(ar);

            long ans = 0;
            for (int i = 0; i < n - 1; i++) {
                sum -= ar[i];
                ans += sum - (n - i - 1) * ar[i];
            }

            System.out.println(ans);

        }
        pw.close();
    }
}

class MOD {
    static long mod = 1000000007;

    static long add(long x, long y) {
        long result = x + y;
        return result > mod ? result - mod : result;
    }

    static long sub(long x, long y) {
        long result = x - y;
        return result < 0 ? result + mod : result;
    }

    static long mul(long x, long y) {
        long result = x * y;
        return result >= mod ? result % mod : result;
    }

    static long pow(long x, long y) {
        long result = 1;
        x %= mod;
        while (y > 0) {
            if ((y & 1) == 1)
                result = mul(result, x);
            x = mul(x, x);
            y = y >>> 1;
        }
        return result;
    }

    static long modInv(long x) {
        return pow(x, mod - 2);
    }

}

class Utility {

    static int ceil(int a, int b) {
        if (a == 0)
            return 0;
        return (a - 1) / b + 1;
    }

    static long ceil(long a, long b) {
        if (a == 0)
            return 0;
        return (a - 1) / b + 1;
    }

    static int gcd(int a, int b) {
        if (a > b) {
            int t = a;
            a = b;
            b = t;
        }
        if (a == 0)
            return b;
        return gcd(b % a, a);
    }

    static long gcd(long a, long b) {
        if (a > b) {
            long t = a;
            a = b;
            b = t;
        }
        if (a == 0)
            return b;
        return gcd(b % a, a);
    }

    static int binarySearch(int[] ar, int x, int start, int end) {
        int l = start;
        int r = end;
        while (l <= r) {
            int mid = l + (r - l) / 2;
            if (ar[mid] == x) {
                return mid;
            } else if (ar[mid] > x) {
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        return -1;
    }

    static int lowerBound(int[] ar, int x) {
        int l = 0;
        int r = ar.length - 1;
        while (l <= r) {
            int mid = l + (r - l) / 2;

            if (ar[mid] >= x)
                r = mid - 1;
            else
                l = mid + 1;
        }
        return r + 1;
    }

    static int upperBound(int[] ar, int x) {
        int l = 0;
        int r = ar.length - 1;
        while (l <= r) {
            int mid = l + (r - l) / 2;

            if (ar[mid] > x)
                r = mid - 1;
            else
                l = mid + 1;
        }
        return l;
    }

    static void sort(int[] a) {
        ArrayList<Integer> l = new ArrayList<>();
        for (int i : a)
            l.add(i);
        Collections.sort(l);
        for (int i = 0; i < a.length; i++)
            a[i] = l.get(i);
    }

    static void sort(long[] a) {
        ArrayList<Long> l = new ArrayList<>();
        for (long i : a)
            l.add(i);
        Collections.sort(l);
        for (int i = 0; i < a.length; i++)
            a[i] = l.get(i);
    }

    static boolean isPalindrome(String s) {
        char[] ss = s.toCharArray();
        int i = 0;
        int j = s.length() - 1;

        while (i < j) {
            if (ss[i] != ss[j]) {
                return false;
            }
            i += 1;
            j -= 1;
        }
        return true;
    }

    static boolean isPrime(int n) {
        if (n <= 1)
            return false;
        else if (n == 2)
            return true;
        else if (n % 2 == 0)
            return false;
        for (int i = 3; i * i <= n; i += 2) {
            if (n % i == 0)
                return false;
        }
        return true;
    }

    static void printArray(int[] ar) {
        for (int i = 0; i < ar.length; i++) {
            System.out.print(ar[i] + " ");
        }
        System.out.println();
    }

    static void printArray(long[] ar) {
        for (int i = 0; i < ar.length; i++) {
            System.out.print(ar[i] + " ");
        }
        System.out.println();
    }

    static void printIntList(List<Integer> list) {
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + " ");
        }
        System.out.println();
    }

    static void printLongList(List<Long> list) {
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + " ");
        }
        System.out.println();
    }

    static long chooseRecursive(long n, long k) {
        if (n < k)
            return 0;
        if (k == 0 || k == n)
            return 1;
        return chooseRecursive(n - 1, k - 1) + chooseRecursive(n - 1, k);
    }

    static long chooseOptimised(long n, long k) {
        long res = 1;
        if (k > n - k)
            k = n - k;
        for (long i = 0; i < k; i++) {
            res *= (n - i);
            res /= (i + 1);
        }
        return res;
    }

    static List<Integer> divisors(int n) {
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i * i < n; i++) {
            if (n % i == 0)
                list.add(i);
        }
        for (int i = (int) Math.sqrt(n); i >= 1; i--) {
            if (n % i == 0)
                list.add(n / i);
        }
        return list;
    }

}

class Pair implements Comparable<Pair> {
    Integer first;
    Integer second;

    Pair(Integer f, Integer s) {
        this.first = f;
        this.second = s;
    }

    public String toString() {
        return "(" + this.first + ", " + this.second + ")";
    }

    @Override
    public boolean equals(Object object) {
        if (((Pair) object).first == this.first && ((Pair) object).second == this.second && object instanceof Pair) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        return (String.valueOf(first) + ":" + String.valueOf(second)).hashCode();
    }

    @Override
    public int compareTo(Pair p) {
        int f = first.compareTo(p.first);
        if (f != 0)
            return f;

        return Integer.compare(second, p.second);
    }
}

class FastScanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");

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

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

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

    int[] getIntArray(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = getInt();
        return a;
    }

    long[] getLongArray(int n) {
        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = getLong();
        return a;
    }

    List<Integer> getIntList(int n) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++)
            list.add(getInt());
        return list;
    }

    List<Long> getLongList(int n) {
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < n; i++)
            list.add(getLong());
        return list;
    }
}
