#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
int n, k;
string s;
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    vector<int> alpha(26, 0);
    vector<int> cpy(26, 0);
    vector<int> cnt(26, 0);
    cin >> s;
    int ans = 0;
    for (char c : s) {
      alpha[c - 'a']++;
      cpy[c - 'a']++;
    }
    for (int i = 1; i <= min(n, k); i++)
      if (k % i == 0)
        for (int l = 1; l <= n; l++) {
          int tmp = 0;
          for (int a = 0; a < 26; a++) tmp += alpha[a] / l;
          if (tmp >= i) ans = max(ans, l * i);
        }
    cout << ans << "\n";
  }
}
