# include using namespace std; namespace Base{ # define mr make_pair typedef long long ll; typedef double db; const int inf = 0x3f3f3f3f, INF = 0x7fffffff; const ll infll = 0x3f3f3f3f3f3f3f3fll, INFll = 0x7fffffffffffffffll; template void read(T &x){ x = 0; int fh = 1; double num = 1.0; char ch = getchar(); while (!isdigit(ch)){ if (ch == '-') fh = -1; ch = getchar(); } while (isdigit(ch)){ x = x * 10 + ch - '0'; ch = getchar(); } if (ch == '.'){ ch = getchar(); while (isdigit(ch)){num /= 10; x = x + num * (ch - '0'); ch = getchar();} } x = x * fh; } template void chmax(T &x, T y){x = x < y ? y : x;} template void chmin(T &x, T y){x = x > y ? y : x;} } using namespace Base; int cnt[11], num[5]; int k, mx, mn, n; char s[11]; void dfs(int k){ if (k == 9){ int mn = inf, mn2 = inf; for (int i = 0; i < 5; i++){ if (mn > num[i]){mn2 = mn; mn = num[i];} else mn2 = min(mn2, num[i]); } mx = max(mx, min(mn + cnt[9], mn2)); return; } for (int i = 0; i < 5; i++){ num[i] += cnt[k]; dfs(k + 1); num[i] -= cnt[k]; } } int main(){ int T; read(T); while (T--){ read(n); memset(cnt, 0, sizeof(cnt)); for (int i = 1; i <= n; i++){ scanf("\n%s", s + 1); cnt[s[5] - '0']++; } sort(cnt, cnt + 9); mx = 0; dfs(0); cout << n - mx << endl; } return 0; }