#include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while(T--) { int n; cin >> n; vector cnt(10); for(int i = 0; i < n; ++i) { string s; cin >> s; cnt[s.back() - '0'] += 1; } int ans = n; vector tot(5); function dfs = [&] (int cur, int c) { if(cur == 10) { ans = min(ans, n - *min_element(tot.begin(), tot.end())); return; } if(c < 4) { tot[c + 1] += cnt[cur]; dfs(cur + 1, c + 1); tot[c + 1] -= cnt[cur]; } for(int i = 0; i <= c; ++i) { tot[i] += cnt[cur]; dfs(cur + 1, c); tot[i] -= cnt[cur]; } }; dfs(0, 0); cout << ans << '\n'; } return 0; }