#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int ans = 0;
    map<char, int> pos;
    for (int i = 0; i < n; i++) {
      pos[s[i]]++;
    }
    for (int i = 1; i <= n; i++) {
      if (k % i == 0) {
        for (int j = 1; j <= n; j++) {
          int len = 0;
          for (int l = 0; l < 26; l++) {
            len += pos['a' + l] / j;
          }
          if (len >= i) {
            ans = max(ans, j * i);
          }
        }
      }
    }
    cout << ans << "\n";
  }
}
