#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n, k;
  cin >> n >> k;
  string second;
  cin >> second;
  long long a[26];
  memset(a, 0, sizeof a);
  for (auto c : second) a[c - 'a']++;
  long long ans = 0;
  for (long long i = 1; i <= k; i++) {
    if (k % i != 0) continue;
    for (long long j = 1; j <= n; j++) {
      long long cnt = 0;
      for (long long p = 0; p <= 25; p++) {
        cnt += (a[p] / j);
      }
      if (cnt >= i) {
        ans = max(ans, i * j);
      }
    }
  }
  cout << ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long t;
  cin >> t;
  while (t--) solve();
}
