#include <bits/stdc++.h>
using namespace std;
int mov[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int cnt[26], cnt1[26];
int n, k;
bool check(int x, int r) {
  int ans = 0;
  for (int i = 25; i >= 26 - x && i >= 0; i--) {
    if (cnt[i] >= r) ans += (cnt[i] / r) * r;
  }
  return (ans >= x * r);
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    string s;
    cin >> s;
    for (int i = 0; i < 26; i++) cnt[i] = 0;
    for (int i = 0; i < s.size(); i++) {
      cnt[s[i] - 'a']++;
    }
    sort(cnt, cnt + 26);
    int ans = 1;
    for (int i = 1; i <= n; i++) {
      if (k % i == 0) {
        for (int j = 1; j * i <= n; j++) {
          if (check(i, j)) {
            ans = max(ans, i * j);
          }
        }
      }
    }
    cout << ans << endl;
  }
}
