#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    map<char, int> mp;
    for (auto it : s) {
      mp[it]++;
    }
    int ans = 1;
    for (int i = 1; i * i <= k; i++) {
      if (k % i == 0) {
        int fact1 = i, fact2 = k / i;
        for (int j = fact1; j <= n; j += fact1) {
          int x = 0;
          for (auto it : mp) x += (it.second / (j / fact1));
          if (x >= fact1) ans = max(ans, j);
        }
        for (int j = fact2; j <= n; j += fact2) {
          int x = 0;
          for (auto it : mp) x += (it.second / (j / fact2));
          if (x >= fact2) ans = max(ans, j);
        }
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
