#include <bits/stdc++.h>
#pragma optimize("Ofast")
using namespace std;
void solve() {
  int n, k;
  string s;
  cin >> n >> k >> s;
  vector<int> cnt(27);
  for (char &c : s) {
    cnt[c - 'a']++;
  }
  int t = 0;
  for (int d = 1; d <= n; d++) {
    vector<int> ncnt = cnt;
    int curr = 0;
    for (int j = 0; j < 26; j++) {
      if (ncnt[j] >= d) {
        ncnt[j] -= d;
        curr++;
        --j;
      }
      if (curr && k % curr == 0) {
        t = max(t, curr * d);
      }
    }
  }
  cout << t << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  int q;
  cin >> q;
  while (q--) {
    solve();
  }
}
