#include <bits/stdc++.h>
long long quickpow(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % 1000000007;
    b >>= 1, a = a * a % 1000000007;
  }
  return ans;
}
using namespace std;
const long long N = 1e3 + 7;
const long long maxn = 2e3 + 9;
long long n, m;
char s[maxn];
long long cnt[27];
bool check(long long x, long long y) {
  for (long long i = 0; i <= 25; i++) {
    y -= cnt[i] / x;
  }
  return y <= 0;
}
void solve() {
  long long n, k;
  cin >> n >> k;
  scanf("%s", s + 1);
  memset(cnt, 0, sizeof(cnt));
  for (long long i = 1; i <= n; i++) {
    cnt[s[i] - 'a']++;
  }
  long long ans = 1;
  for (long long i = 1; i <= k; i++) {
    if (k % i == 0) {
      long long l = 1, r = n;
      while (r >= l) {
        long long mid = (l + r) >> 1;
        if (check(mid, i)) {
          l = mid + 1;
        } else {
          r = mid - 1;
        }
      }
      ans = max(ans, r * i);
    }
  }
  cout << ans << endl;
}
signed main() {
  long long _;
  cin >> _;
  while (_--) solve();
}
