#include <bits/stdc++.h>
using namespace std;
int cansel_sync = (ios::sync_with_stdio(0), cin.tie(0), 0);
const int N = 200010;
const int inf = ~0u >> 2;
const long long INF = ~0ull >> 2;
long long read() {
  long long x;
  if (scanf("%lld", &x) == -1) exit(0);
  return x;
}
const double pi = acos(-1.0);
double readf() {
  double x;
  if (scanf("%lf", &x) == -1) exit(0);
  return x;
}
template <typename T>
T sqr(const T &x) {
  return x * x;
}
const int mod = (1 ? 1000000007 : 998244353);
long long mul(long long a, long long b, long long m = mod) { return a * b % m; }
long long qpow(long long a, long long b, long long m = mod) {
  long long ans = 1;
  for (; b; a = mul(a, a, m), b >>= 1)
    if (b & 1) ans = mul(ans, a, m);
  return ans;
}
long long getinv(long long v, long long m = mod) { return qpow(v, m - 2, m); }
long long n, k;
string s;
map<char, long long> mp;
long long work(long long k) {
  long long l = 1, r = 2001;
  while (l <= r) {
    long long m = (l + r) / 2;
    long long cnt = 0;
    for (auto i : mp) cnt += i.second / m;
    if (cnt < k)
      r = m - 1;
    else
      l = m + 1;
  }
  return r * k;
}
signed main() {
  long long T;
  cin >> T;
  while (T--) {
    cin >> n >> k;
    cin >> s;
    mp.clear();
    for (auto i : s) mp[i]++;
    long long ans = 0;
    for (auto i : mp) ans = max(ans, i.second);
    for (long long i = 1; i <= k; i++)
      if (k % i == 0) ans = max(ans, work(i));
    cout << ans << endl;
  }
  return 0;
}
