t = int(input())
for x in range(t) :
n=int(input())
a=list(map(int,input().split()))
c0,c1,c2=0,0,0
for i in a :
if i%3==0 :
c0+=1
elif i%3==1:
c1+=1
else :
c2+=1
res=0
have=n//3
while (c0 != c1) or (c0 != c2) or (c2 != c1) :
if c0>have :
res+=c0-have
c1+=c0-have
c0-=c0-have
elif c1>have:
res+=c1-have
c2+=c1-have
c1-=c1-have
elif c2>have :
res+=c2-have
c0+=c2-have
c2-=c2-have
print(res)
#include <bits/stdc++.h>
using namespace std;
void HOFNY() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); }
#define ll long long
#define sz(v) (int)v.size()
#define all(x) x.begin(), x.end()
#define h "\n"
#define m(frq,x) memset(frq,x,sizeof(frq))
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
int main() {
HOFNY();
int tc = 1; cin >> tc;
while (tc--) {
int n,cont=0; cin >> n;
vector<int>v(n);
int c1=0, c2=0, c0=0;
for (int i = 0; i < n; i++) {
cin >> v[i];
if (v[i] % 3 == 0)c0++;
else if (v[i]%3==1)c1++;
else c2++;
}
int mx = max({ c0,c1,c2 });
int mn = min({ c0,c1,c2 });
while (mx > mn) {
int mx = max({ c0,c1,c2 });
int mn = min({ c0,c1,c2 });
if (mx == mn)break;
if (mx == c0) {
if (mn == c1) {
c0--;
c1++;
cont++;
}
else {
c0--;
c2++;
cont+=2;
}
}
else if (mx == c1) {
if (mn == c0) {
c1--;
c0++;
cont+=2;
}
else {
c1--;
c2++;
cont ++;
}
}
else if (mx == c2) {
if (mn == c0) {
c2--;
c0++;
cont++;
}
else {
c2--;
c1++;
cont += 2;
}
}
}
cout << cont<<h;
}
}
237. Delete Node in a Linked List | 27. Remove Element |
39. Combination Sum | 378. Kth Smallest Element in a Sorted Matrix |
162. Find Peak Element | 1529A - Eshag Loves Big Arrays |
19. Remove Nth Node From End of List | 925. Long Pressed Name |
1051. Height Checker | 695. Max Area of Island |
402. Remove K Digits | 97. Interleaving String |
543. Diameter of Binary Tree | 124. Binary Tree Maximum Path Sum |
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts | 501A - Contest |
160A- Twins | 752. Open the Lock |
1535A - Fair Playoff | 1538F - Interesting Function |
1920. Build Array from Permutation | 494. Target Sum |
797. All Paths From Source to Target | 1547B - Alphabetical Strings |
1550A - Find The Array | 118B - Present from Lena |
27A - Next Test | 785. Is Graph Bipartite |
90. Subsets II | 1560A - Dislike of Threes |