#include <bits/stdc++.h>
using namespace std;
int get(int n, int k) {
  if (n <= 0) return 0;
  return ceil(double(n) / (k + 1));
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int d[30];
    memset(d, 0, sizeof(d));
    for (char c : s) d[c - 'a']++;
    int res = 0;
    for (int ii = 1; ii <= sqrt(k); ii++)
      if (k % ii == 0) {
        int i = ii;
        for (int j = 1; j <= n / i; j++) {
          int cnt = 0;
          for (int k = 0; k < 26; k++) cnt += d[k] / j;
          if (cnt >= i) res = max(res, i * j);
        }
        if (i * i == k) continue;
        i = k / ii;
        for (int j = 1; j <= n / i; j++) {
          int cnt = 0;
          for (int k = 0; k < 26; k++) cnt += d[k] / j;
          if (cnt >= i) res = max(res, i * j);
        }
      }
    cout << res << endl;
  }
}
