#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c *x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct deb {
  template <class c>
  deb &operator<<(const c &) {
    return *this;
  }
};
template <class T>
T &chmin(T &a, const T &b) {
  return a = min(a, b);
}
template <class T>
T &chmax(T &a, const T &b) {
  return a = max(a, b);
}
const long long dx[8] = {0, 1, -1, 0, 1, 1, -1, -1};
const long long dy[8] = {1, 0, 0, -1, 1, -1, -1, 1};
const long long mod = 1e9 + 7;
inline long long add(long long a, long long b) {
  return ((a % mod) + (b % mod)) % mod;
}
inline long long sub(long long a, long long b) {
  return ((a % mod) - (b % mod) + mod) % mod;
}
inline long long mul(long long a, long long b) {
  return ((a % mod) * (b % mod)) % mod;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec) is >> v;
  return is;
}
const double PI = acos(-1.0);
const long long inf = 1e18;
const long long maxn = 1e3 + 5;
vector<long long> factors;
bool check(vector<long long> &mp, long long p) {
  for (auto f : factors) {
    if (p % f == 0) {
      long long tot = f;
      long long req = p / f;
      for (long long i = (long long)mp.size() - 1; i >= 0; i--) {
        if (mp[i] < req) break;
        tot -= (mp[i] / req);
        if (tot <= 0) return true;
      }
    }
  }
  return false;
}
void solve() {
  long long n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  factors = vector<long long>();
  map<long long, long long> mp;
  for (auto ch : s) mp[ch]++;
  vector<long long> f;
  for (auto i : mp) {
    f.push_back(i.second);
  }
  long long m = (long long)f.size();
  sort(f.begin(), f.end());
  long long ans = 1;
  for (long long i = 1; i * i <= k; i++) {
    if (k % i == 0) {
      long long k1 = i;
      long long k2 = k / i;
      factors.push_back(k1);
      if (k2 != k1) factors.push_back(k2);
    }
  }
  for (long long p = n; p >= 2; p--) {
    long long tot = 0;
    bool ok = check(f, p);
    if (ok) {
      ans = p;
      break;
    }
  }
  cout << ans << "\n";
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  cin >> t;
  for (long long tt = 1; tt <= t; tt++) {
    solve();
  }
}
