#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    vector<long long> cnt(26);
    for (auto u : s) ++cnt[u - 'a'];
    long long ans = 0;
    for (long long period = 1; period <= n; ++period) {
      if (k % period) continue;
      for (long long times = 1; times <= n; ++times) {
        long long length = 0;
        for (long long c = 0; c < 26; ++c) length += cnt[c] / times;
        if (length < period) continue;
        ans = max(ans, period * times);
      }
    }
    cout << ans << '\n';
  }
}
