#include <bits/stdc++.h>
using namespace std;
int N, K, Cnt[26];
inline void pre() {
  string s;
  (cin >> N >> K).get();
  cin >> s;
  for (auto &c : Cnt) c = 0;
  for (const auto c : s) Cnt[c - 'a']++;
}
inline int ans() {
  pre();
  int Ans = 0;
  for (int l = 1; l <= N; l++)
    if (K % l == 0)
      for (int i = 1, j = l; j <= N; i++, j += l) {
        int MaxLen = 0;
        for (auto c : Cnt) MaxLen += c / i;
        if (MaxLen >= l) Ans = max(Ans, j);
      }
  return Ans;
}
int main() {
  int T;
  cin >> T;
  for (int i = 1; i <= T; i++) cout << ans() << endl;
  return 0;
}
