#include <bits/stdc++.h>   using namespace std;   #pragma GCC optimize( O3 )   #pragma GCC target( avx )   #pragma GCC optimize( Ofast )   #pragma GCC optimize( unroll-loops )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   const int MOD = 1000000007;   const int INF = 1000000009;   const int MAXN = 1000005;   vector<string> split(const string& s, char c) {     vector<string> v;     stringstream ss(s);     string x;     while (getline(ss, x, c)) v.emplace_back(x);     return move(v);   }   template <typename T, typename... Args>   inline string arrStr(T arr, int n) {     stringstream s;     s <<  [ ;     for (int i = 0; i < n - 1; i++) s << arr[i] <<  , ;     s << arr[n - 1] <<  ] ;     return s.str();   }   bool is_prime(long long n) {     if (n < 2) return false;     for (long long x = 2; x * x <= n; x++) {       if (n % x == 0) return false;     }     return true;   }   vector<long long> get_prime_factors(long long n) {     vector<long long> f;     for (long long x = 2; x * x <= n; x++) {       while (n % x == 0) {         f.push_back(x);         n /= x;       }     }     if (n > 1) f.push_back(n);     return f;   }   long long fpow(long long x, long long n) {     long long res = 1;     while (n) {       if (n & 1) {         res = (1LL * res * x) % MOD;       }       x = (1LL * x * x) % MOD;       n >>= 1;     }     return (res % MOD);   }   vector<long long> fac, inv;   void create_fac(long long n = MAXN) {     fac.resize(n + 5);     inv.resize(n + 5);     fac[0] = 1;     inv[0] = fpow(fac[0], MOD - 2);     for (long long i = 1; i <= n; i++) {       fac[i] = (fac[i - 1] % MOD * i % MOD) % MOD;       inv[i] = fpow(fac[i], MOD - 2);     }   }   long long binomial_coefficient(long long n, long long r) {     long long res = 1;     res = (res * (fac[n] % MOD)) % MOD;     res = (res * (inv[r] % MOD)) % MOD;     res = (res * (inv[n - r] % MOD)) % MOD;     return res;   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }   long long sceil(long long a, long long b) { return (a + b - 1) / b; }   vector<long long> lp;   vector<long long> pr;   void sieve(long long n = MAXN) {     lp.resize(n + 1);     pr.clear();     for (long long i = 2; i <= n; ++i) {       if (lp[i] == 0) {         lp[i] = i;         pr.push_back(i);       }       for (long long j = 0;            j < (long long)pr.size() && pr[j] <= lp[i] && i * pr[j] <= n; ++j)         lp[i * pr[j]] = pr[j];     }   }   long long extended_euclidean(long long a, long long b, long long& x,                                long long& y) {     if (b == 0) {       x = 1;       y = 0;       return a;     }     long long x1, y1;     long long d = extended_euclidean(b, a % b, x1, y1);     x = y1;     y = x1 - y1 * (a / b);     return d;   }   int mod_inv(long long a, long long m) {     long long x, y;     long long g = extended_euclidean(a, m, x, y);     if (g != 1) {       return -1;     } else {       x = (x % m + m) % m;       return x;     }   }   long long solution() {     long long n, k;     cin >> n >> k;     string s;     cin >> s;     long long ans = 0;     if (k <= n) ans = k;     vector<long long> f(26, 0);     for (auto c : s) {       f[c -  a ]++;     }     long long MX = *max_element(f.begin(), f.end());     vector<long long> v(MX, 0);     for (int times = MX; times > 0; times--) {       for (int i = 0; i < 26; i++) {         v[times - 1] += f[i] / times;       }     }     reverse(v.begin(), v.end());     42;     for (long long i = 1; i <= k && i <= n; i++) {       if (k % i == 0) {         long long times = MX - (lower_bound(v.begin(), v.end(), i) - v.begin());         42;         42;         ans = max(ans, times * i);       }     }     return ans;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int tt = 1;     cin >> tt;     while (tt--) {       cout << solution() <<   n ;     }     return 0;   }   