#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<ll> dp(100005, -1);
ll gcd(ll a, ll b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
int can(ll arr[], int l, ll k) {
  k = gcd(l, k);
  ll g = l / k;
  for (int i = 25; i >= 0; i--) {
    ll t = arr[i] / g;
    k -= t;
    if (k <= 0) return 1;
  }
  return 0;
}
void solve() {
  ll n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  ll arr[26] = {0};
  for (auto e : s) arr[e - 'a']++;
  ll ans = 0;
  for (auto e : arr) ans = max(ans, e);
  sort(arr, arr + 26);
  for (int i = 1; i <= n; i++) {
    if (can(arr, i, k)) ans = i;
  }
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  ll t;
  t = 1;
  cin >> t;
  while (t--) solve();
}
