#include <bits/stdc++.h>
using namespace std;
long long t, n, k, f[30], vv, mx, ans;
string s;
vector<long long> v;
int main() {
  cin >> t;
  while (t--) {
    memset(f, 0, sizeof f);
    cin >> n >> k >> s;
    v.clear();
    for (int i = 0; i < n; i++) f[s[i] - 'a']++;
    for (int i = 1; i <= k; i++) {
      if (k % i == 0) v.push_back(i);
    }
    mx = 0;
    for (int i = 0; i < v.size(); i++) {
      for (int j = 1; j * v[i] <= n; j++) {
        ans = 0;
        for (int z = 0; z < 26; z++) ans += f[z] / j;
        if (ans >= v[i]) mx = max(mx, j * v[i]);
      }
    }
    cout << mx << endl;
  }
}
