#include <bits/stdc++.h>
using namespace std;
const long long OO = (long long)(4e18) + 9;
const int MOD = (int)(1e9) + 7;
const int oo = 2147483647;
const double EPS = 1e-8;
const double PI = acos(-1.0);
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int dX[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dY[] = {0, 1, 0, -1, 1, 1, -1, -1};
string abc = "abcdefghijklmnopqrstuvwxyz";
string vowels = "aeiou";
template <typename X, typename Y>
bool ckmin(X& a, Y b) {
  return a > b ? a = b, true : false;
}
template <typename X, typename Y>
bool ckmax(X& a, Y b) {
  return a < b ? a = b, true : false;
}
int dcmp(long double d1, long double d2) {
  return fabs(d1 - d2) <= EPS ? 0 : d1 > d2 ? 1 : -1;
}
long long gcd(long long x, long long y) { return !y ? x : gcd(y, x % y); }
long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }
int conv_2_1(int r, int c, int nCol) { return (nCol * r) + c + 1; }
pair<int, int> conv_1_2(int n, int nCol) {
  int r = n / nCol, c = n % nCol;
  return pair<int, int>(r, c);
}
long long poww(long long num, long long p, long long md = MOD) {
  num %= md;
  long long ret = 1;
  while (p > 0) {
    if (p & 1) ret = (ret * num) % md;
    num = (num * num) % md;
    p >>= 1;
  }
  return ret;
}
void inter(string second) {
  cout << second << endl;
  fflush(stdout);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    string second;
    cin >> n >> k >> second;
    vector<int> cnt(26);
    for (int i = 0; i < n; i++) cnt[second[i] - 'a']++;
    for (int ans = n; ans; ans--) {
      int repeat = ans / gcd(ans, k);
      int sum = 0;
      for (auto c : cnt) sum += c / repeat;
      if (sum >= gcd(ans, k)) {
        cout << ans << endl;
        break;
      }
    }
  }
  return 0;
}
