#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int freq[26] = {0};
    vector<int> v;
    for (int i = 0; i < s.length(); ++i) {
      freq[s[i] - 'a']++;
    }
    for (int i = 0; i < 26; ++i) {
      v.push_back(freq[i]);
    }
    sort(v.begin(), v.end());
    int maxAns = 1;
    for (int i = 1; i <= k; ++i) {
      if (k % i == 0) {
        for (int j = i; j <= n; j += i) {
          int ans = 0;
          int div = j / i;
          for (int k = 0; k < v.size(); ++k) {
            ans += v[k] / div;
          }
          if (ans >= i) {
            maxAns = max(maxAns, j);
          }
        }
      }
    }
    cout << maxAns << endl;
  }
}
