#include <bits/stdc++.h>
using namespace std;
void err(istringstream *iss) {}
template <typename T, typename... Args>
void err(istringstream *iss, const T &_val, const Args &...args) {
  string _name;
  *iss >> _name;
  if (_name.back() == ',') _name.pop_back();
  cerr << _name << " = " << _val << "; ", err(iss, args...);
}
const long double PI = acosl(-1.0);
void solve() {
  long long n, k;
  cin >> n >> k;
  string str;
  cin >> str;
  vector<long long> factors;
  long long freq[26] = {0};
  for (long long i = 1; i <= sqrt(k); i++) {
    if (k % i == 0) factors.push_back(i), factors.push_back(k / i);
  }
  for (long long i = 0; i < n; i++) freq[str[i] - 'a']++;
  long long ans = 0;
  for (long long i = 0; i < factors.size(); i++) {
    long long f = factors[i];
    for (long long j = f; j <= n; j += f) {
      long long freach = j / f;
      long long cnt = 0;
      for (long long p = 0; p < 26; p++) {
        cnt += freq[p] / freach;
      }
      if (cnt >= f) ans = max(ans, j);
    }
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
