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

public final class Codechef {
	public static void main (String[] args) {
	    Scanner scanner = new Scanner(System.in);
	    int numTestcases = scanner.nextInt();
	    
	    for(int test=1; test<=numTestcases; test++) {
	        int n = scanner.nextInt();
	        int[] arr = new int[n];
	        long sum = 0;
	        for(int arrIdx=0; arrIdx < n; arrIdx++) {
	            arr[arrIdx] = scanner.nextInt();
	            sum = (long) sum + (long) arr[arrIdx];
	        }
	        
	        if(sum % n == 0) {
	            System.out.println(0);
	        } else {
	            System.out.println((long)((((sum/n) + 1L) * (long) n) - (long) sum) * (long) (sum % n));
	        }
	    }
	}
	
}