#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const int MOD = 1e9 + 7;
const int INF32 = 1 << 30;
const long long INF64 = 1LL << 60;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    vector<int> fr(26, 0);
    int ans = 0;
    for (int i = 0; i < n; i++) {
      char c;
      cin >> c;
      fr[c - 'a']++;
    }
    for (int i = 1; i * i <= k; i++)
      if (k % i == 0) {
        for (int j = 1; j <= n / i; j++) {
          int cnt = 0;
          for (int l = 0; l < 26; l++) cnt += fr[l] / j;
          if (cnt >= i) ans = max(ans, i * j);
        }
        for (int j = 1; j <= n / (k / i); j++) {
          int cnt = 0;
          for (int l = 0; l < 26; l++) cnt += fr[l] / j;
          if (cnt >= k / i) ans = max(ans, k / i * j);
        }
      }
    cout << ans << endl;
  }
  return 0;
}
