#include <bits/stdc++.h>
using namespace std;
int gcd(int b, int c) {
  if (b == 0) {
    return c;
  }
  if (b >= c) {
    return gcd(b % c, c);
  } else {
    return gcd(c % b, b);
  }
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k, i;
    cin >> n >> k;
    string s;
    cin >> s;
    int a[26] = {0};
    for (i = 0; i < n; i++) {
      a[s[i] - 'a']++;
    }
    sort(a, a + 26);
    int len = n + 1;
    while (--len) {
      if (k % len == 0) {
        cout << len << '\n';
        break;
      }
      int den = gcd(k, len);
      int parts = len / den;
      int index = lower_bound(a, a + 26, parts) - a;
      int gg = 0;
      for (i = index; i < 26; i++) {
        gg += a[i] / parts;
      }
      if (gg >= den) {
        cout << len << '\n';
        break;
      }
    }
  }
  return 0;
}
