t=int(input())
for i in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    a.sort()
    i=0
    j=n-1
    while (i<j):
        res=a[i]+a[j]
        if res%2==0:
            a[i]=a[j]=res//2
        else:
            a[i]=(res//2)+1
            a[j]=res//2
        i+=1
        j-=1
    ans=0
    for i in range(n-1):
        for j in range(i+1,n):
            ans+=abs(a[i]-a[j])
    print(ans)