#include <bits/stdc++.h>
using namespace std;
long long n, k, cur_div;
vector<long long> cnt;
bool check(long long x) {
  long long tot = 0;
  for (long long i = 0; i < 26; i++) {
    tot += cnt[i] / x;
    if (tot >= cur_div) {
      return true;
    }
  }
  return false;
}
void solve() {
  string second;
  cin >> n >> k;
  cin >> second;
  if (n == k) {
    cout << n << '\n';
    return;
  }
  vector<long long> divs;
  for (long long i = 1; i * i <= k; i++) {
    if (k % i) continue;
    long long div0 = i, div1 = k / i;
    divs.push_back(div0);
    if (div0 != div1) {
      divs.push_back(div1);
    }
  }
  cnt.assign(26, 0);
  for (char &c : second) {
    cnt[c - 'a']++;
  }
  long long ans = 1;
  for (long long i = 0; i < divs.size(); i++) {
    cur_div = divs[i];
    long long l = 1, r = n / cur_div;
    while (l <= r) {
      long long mid = (l + r) >> 1;
      if (check(mid)) {
        ans = max(ans, mid * cur_div);
        l = mid + 1;
      } else {
        r = mid - 1;
      }
    }
  }
  cout << ans << '\n';
}
signed main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
}
