#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, k;
    scanf("%d %d", &n, &k);
    string s;
    cin >> s;
    map<char, int> mp;
    int mm = 0;
    for (int i = 0; i <= n - 1; i++) {
      mp[s[i]]++;
      mm = max(mm, mp[s[i]]);
    }
    int mx = 0;
    for (int i = 1; i <= n; i++) {
      if (k % i == 0) {
        for (int j = 1; j <= mm; j++) {
          int sum = 0;
          for (int k = 'a'; k <= 'z'; k++) {
            sum += (mp[k] / j);
          }
          if (sum >= i) mx = max(mx, i * j);
        }
      }
      if (i > k) break;
    }
    printf("%d\n", mx);
  }
}
