#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T &a, const T &b) {
  return b > a ? a = b, true : false;
}
template <class T>
inline bool chmin(T &a, const T &b) {
  return b < a ? a = b, true : false;
}
template <class T>
using MaxHeap = priority_queue<T>;
template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template <class T, class F = less<T>>
void sort_uniq(vector<T> &v, F f = F()) {
  sort(begin(v), end(v), f);
  v.resize(unique(begin(v), end(v)) - begin(v));
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    map<char, int> mp;
    vector<int> a;
    for (int i = 0; i < (int)(n); i++) mp[s[i]]++;
    for (auto p : mp) {
      a.push_back(p.second);
    }
    sort(begin(a), end(a));
    int m = a.size();
    int ans = 0;
    vector<int> p;
    for (int i = 1; i * i <= k; i++) {
      if (k % i == 0) {
        p.push_back(i);
        if (k / i != i) p.push_back(k / i);
      }
    }
    for (int l = 1; l <= a.back(); l++) {
      int sum = 0;
      for (int i : a) {
        sum += i / l;
      }
      for (int j : p) {
        if (sum >= j) chmax(ans, j * l);
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
