#include <bits/stdc++.h>
using namespace std;
long long powe(long long n, long long m) {
  if (m == 0) return 1;
  long long t = powe(n, m / 2);
  if (m % 2 == 0) return (t * t);
  return (((t * t)) * n);
}
long long mpowe(long long n, long long m, long long MOD) {
  if (m == 0) return 1;
  long long t = mpowe(n, m / 2, MOD);
  t %= MOD;
  if (m % 2 == 0) return (t * t) % MOD;
  return (((t * t) % MOD) * n) % MOD;
}
long long logtwo(long long n) {
  if (n == 1) return 0;
  if (n == 0) return -1;
  return logtwo(n / 2) + 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int freq[26] = {0};
    for (int i = 0; i < n; i++) {
      int d = (int)(s[i] - 'a');
      freq[d]++;
    }
    std::vector<int> v;
    int kk = k;
    for (int i = 1; i <= kk; i++) {
      if (kk % i == 0) {
        v.push_back(i);
      }
    }
    int ans = 0;
    for (int i = 0; i < v.size(); i++) {
      for (int j = 1; j * v[i] <= n; j++) {
        long long pos = 0;
        for (int l = 0; l < 26; l++) {
          pos += freq[l] / j;
        }
        if (pos >= v[i]) {
          ans = max(ans, j * v[i]);
        }
      }
    }
    cout << ans << endl;
  }
}
