#include using namespace std; vector a(1e4 + 1); vector cnt(10), f(10), cur(6); int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int tt; cin >> tt; while (tt--) { int n; cin >> n; fill(cnt.begin(), cnt.end(), 0); for (int i = 0; i < n; i++) { cin >> a[i]; cnt[a[i].back() - '0']++; } int ans = n; function dfs = [&](int step, int res) { if (step == 10) { ans = min(ans, n - *min_element(cur.begin() + 1, cur.end())); return; } for (int i = 1; i <= 5; i++) { f[step] = i; cur[i] += cnt[step]; dfs(step + 1, i == 0 ? res : min(res, cur[i])); cur[i] -= cnt[step]; } }; dfs(0, 0); cout << ans << '\n'; } return 0; }