//raja1999

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")

#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <utility>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <iomanip>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//setbase - cout << setbase (16)a; cout << 100 << endl; Prints 64
//setfill -   cout << setfill ('x') << setw (5); cout << 77 <<endl;prints xxx77
//setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
//cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val

using namespace std;
using namespace __gnu_pbds;
#define f(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) f(i,0,n)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define vi vector< int >
#define vl vector< ll >
#define ss second
#define ff first
#define ll long long
#define pii pair< int,int >
#define pll pair< ll,ll >
#define sz(a) a.size()
#define inf (1000*1000*1000+5)
#define all(a) a.begin(),a.end()
#define tri pair<int,pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000*1000*1000+7)
#define pqueue priority_queue< int >
#define pdqueue priority_queue< int,vi ,greater< int > >
#define int ll

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

//std::ios::sync_with_stdio(false);
multiset<int>se;
multiset<int>::iterator it;
int cnt[30],vis[100];
main(){
	std::ios::sync_with_stdio(false); cin.tie(NULL);
	int t;
	cin>>t;
	while(t--){
		int n,k,i,j,val,c;
		vi vec;
		cin>>n>>k;
		string s;
		cin>>s;
		rep(i,30){
			cnt[i]=0;
		}
		rep(i,s.length()){
			cnt[s[i]-'a']++;
		}
		rep(i,26){
			if(cnt[i]!=0){
				se.insert(cnt[i]);
			}
		}
		fd(i,n,1){
			vec.clear();
			f(j,0,i){
				vis[j]=0;
			}
			f(j,0,i){
				c=0;
				while(vis[j]==0){
					vis[j]=1;
					c++;
					j+=k;
					j=j%i;
				}
				if(c!=0)
					vec.pb(c);
			}
			sort(vec.rbegin(),vec.rend());
			rep(j,vec.size()){
				if(se.size()==0){
					break;
				}
				it=se.end();it--;
				if(*it<vec[j]){
					break;
				}
				val=*it;
				se.erase(se.find(*it));
				se.insert(val-vec[j]);
			}
			if(j==vec.size()){
				cout<<i<<endl;
				break;
			}
		}
	}
	return 0;
}


//raja1999

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")

#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <utility>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <iomanip>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//setbase - cout << setbase (16)a; cout << 100 << endl; Prints 64
//setfill -   cout << setfill ('x') << setw (5); cout << 77 <<endl;prints xxx77
//setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
//cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val

using namespace std;
using namespace __gnu_pbds;
#define f(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) f(i,0,n)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define vi vector< int >
#define vl vector< ll >
#define ss second
#define ff first
#define ll long long
#define pii pair< int,int >
#define pll pair< ll,ll >
#define sz(a) a.size()
#define inf (1000*1000*1000+5)
#define all(a) a.begin(),a.end()
#define tri pair<int,pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000*1000*1000+7)
#define pqueue priority_queue< int >
#define pdqueue priority_queue< int,vi ,greater< int > >
#define int ll

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

//std::ios::sync_with_stdio(false);
multiset<int>se,se1;
multiset<int>::iterator it;
int cnt[30],vis[100];
main(){
	std::ios::sync_with_stdio(false); cin.tie(NULL);
	int t;
	cin>>t;
	while(t--){
		int n,k,i,j,val,c;
		vi vec;
		cin>>n>>k;
		string s;
		cin>>s;
		rep(i,30){
			cnt[i]=0;
		}
		rep(i,s.length()){
			cnt[s[i]-'a']++;
		}
		rep(i,26){
			if(cnt[i]!=0){
				se1.insert(cnt[i]);
			}
		}
		fd(i,n,1){
			vec.clear();
			f(j,0,i){
				vis[j]=0;
			}
			f(j,0,i){
				c=0;
				while(vis[j]==0){
					vis[j]=1;
					c++;
					j+=k;
					j=j%i;
				}
				if(c!=0)
					vec.pb(c);
			}
			sort(vec.rbegin(),vec.rend());
			se=se1;
			rep(j,vec.size()){
				if(se.size()==0){
					break;
				}
				it=se.end();it--;
				if(*it<vec[j]){
					break;
				}
				val=*it;
				se.erase(se.find(*it));
				se.insert(val-vec[j]);
			}
			//cout<<j<<" "<<vec.size()<<endl;
			if(j==vec.size()){
				cout<<i<<endl;
				break;
			}
		}
	}
	return 0;
}


//raja1999

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")

#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <utility>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <iomanip>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//setbase - cout << setbase (16)a; cout << 100 << endl; Prints 64
//setfill -   cout << setfill ('x') << setw (5); cout << 77 <<endl;prints xxx77
//setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
//cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val

using namespace std;
using namespace __gnu_pbds;
#define f(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) f(i,0,n)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define vi vector< int >
#define vl vector< ll >
#define ss second
#define ff first
#define ll long long
#define pii pair< int,int >
#define pll pair< ll,ll >
#define sz(a) a.size()
#define inf (1000*1000*1000+5)
#define all(a) a.begin(),a.end()
#define tri pair<int,pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000*1000*1000+7)
#define pqueue priority_queue< int >
#define pdqueue priority_queue< int,vi ,greater< int > >
#define int ll

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

//std::ios::sync_with_stdio(false);
multiset<int>se,se1;
multiset<int>::iterator it;
int cnt[30],vis[100];
main(){
	std::ios::sync_with_stdio(false); cin.tie(NULL);
	int t;
	cin>>t;
	while(t--){
		int n,k,i,j,val,c;
		vi vec;
		cin>>n>>k;
		string s;
		cin>>s;
		rep(i,30){
			cnt[i]=0;
		}
		rep(i,s.length()){
			cnt[s[i]-'a']++;
		}
		rep(i,26){
			if(cnt[i]!=0){
				se1.insert(cnt[i]);
			}
		}
		fd(i,n,1){
			vec.clear();
			f(j,0,i){
				vis[j]=0;
			}
			f(j,0,i){
				c=0;
				while(vis[j]==0){
					vis[j]=1;
					c++;
					j+=k;
					j=j%i;
				}
				if(c!=0)
					vec.pb(c);
			}
			sort(vec.rbegin(),vec.rend());
			se=se1;
			rep(j,vec.size()){
				if(se.size()==0){
					break;
				}
				it=se.end();it--;
				if(*it<vec[j]){
					break;
				}
				val=*it;
				se.erase(se.find(*it));
				se.insert(val-vec[j]);
			}
			cout<<j<<" "<<vec.size()<<endl;
			if(j==vec.size()){
				cout<<i<<endl;
				break;
			}
		}
	}
	return 0;
}


//raja1999

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")

#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <utility>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <iomanip>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//setbase - cout << setbase (16)a; cout << 100 << endl; Prints 64
//setfill -   cout << setfill ('x') << setw (5); cout << 77 <<endl;prints xxx77
//setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
//cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val

using namespace std;
using namespace __gnu_pbds;
#define f(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) f(i,0,n)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define vi vector< int >
#define vl vector< ll >
#define ss second
#define ff first
#define ll long long
#define pii pair< int,int >
#define pll pair< ll,ll >
#define sz(a) a.size()
#define inf (1000*1000*1000+5)
#define all(a) a.begin(),a.end()
#define tri pair<int,pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000*1000*1000+7)
#define pqueue priority_queue< int >
#define pdqueue priority_queue< int,vi ,greater< int > >
#define int ll

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

//std::ios::sync_with_stdio(false);
multiset<int>se,se1;
multiset<int>::iterator it;
int cnt[30],vis[20000];
main(){
	std::ios::sync_with_stdio(false); cin.tie(NULL);
	int t;
	cin>>t;
	while(t--){
		int n,k,i,j,val,c;
		vi vec;
		cin>>n>>k;
		se1.clear();
		string s;
		cin>>s;
		rep(i,30){
			cnt[i]=0;
		}
		rep(i,s.length()){
			cnt[s[i]-'a']++;
		}
		rep(i,26){
			if(cnt[i]!=0){
				se1.insert(cnt[i]);
			}
		}
		fd(i,n,1){
			vec.clear();
			f(j,0,i){
				vis[j]=0;
			}
			f(j,0,i){
				c=0;
				while(vis[j]==0){
					vis[j]=1;
					c++;
					j+=k;
					j=j%i;
				}
				if(c!=0)
					vec.pb(c);
			}
			sort(vec.rbegin(),vec.rend());
			se=se1;
			rep(j,vec.size()){
				if(se.size()==0){
					break;
				}
				it=se.end();it--;
				if(*it<vec[j]){
					break;
				}
				val=*it;
				se.erase(se.find(*it));
				se.insert(val-vec[j]);
			}
			//cout<<j<<" "<<vec.size()<<endl;
			if(j==vec.size()){
				cout<<i<<endl;
				break;
			}
		}
	}
	return 0;
}


//raja1999

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")

#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <utility>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <iomanip>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//setbase - cout << setbase (16)a; cout << 100 << endl; Prints 64
//setfill -   cout << setfill ('x') << setw (5); cout << 77 <<endl;prints xxx77
//setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
//cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val

using namespace std;
using namespace __gnu_pbds;
#define f(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) f(i,0,n)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define vi vector< int >
#define vl vector< ll >
#define ss second
#define ff first
#define ll long long
#define pii pair< int,int >
#define pll pair< ll,ll >
#define sz(a) a.size()
#define inf (1000*1000*1000+5)
#define all(a) a.begin(),a.end()
#define tri pair<int,pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000*1000*1000+7)
#define pqueue priority_queue< int >
#define pdqueue priority_queue< int,vi ,greater< int > >
#define int ll

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

//std::ios::sync_with_stdio(false);
multiset<int>se,se1;
multiset<int>::iterator it;
int cnt[30],vis[100];
main(){
	std::ios::sync_with_stdio(false); cin.tie(NULL);
	int t;
	cin>>t;
	while(t--){
		int n,k,i,j,val,c;
		vi vec;
		cin>>n>>k;
		se1.clear();
		string s;
		cin>>s;
		rep(i,30){
			cnt[i]=0;
		}
		rep(i,s.length()){
			cnt[s[i]-'a']++;
		}
		rep(i,26){
			if(cnt[i]!=0){
				se1.insert(cnt[i]);
			}
		}
		fd(i,n,1){
			vec.clear();
			f(j,0,i){
				vis[j]=0;
			}
			f(j,0,i){
				c=0;
				while(vis[j]==0){
					vis[j]=1;
					c++;
					j+=k;
					j=j%i;
				}
				if(c!=0)
					vec.pb(c);
			}
			sort(vec.rbegin(),vec.rend());
			se=se1;
			rep(j,vec.size()){
				if(se.size()==0){
					break;
				}
				it=se.end();it--;
				if(*it<vec[j]){
					break;
				}
				val=*it;
				se.erase(se.find(*it));
				se.insert(val-vec[j]);
			}
			//cout<<j<<" "<<vec.size()<<endl;
			if(j==vec.size()){
				cout<<i<<endl;
				break;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define mem(dp,a) memset(dp,a,sizeof(dp))
#define pb(x) push_back(x)
#define m_p(x,y) make_pair(x,y)
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define per(i,b,a) for (ll i=b;i>=a;i--)
#define all(v) (v).begin(),(v).end()
#define ff first
#define ss second
#define pi acosl(-1.0l)
#define hs ios_base::sync_with_stdio(false);cin.tie(NULL);
#define fixed(n) fixed<<setprecision(n)
using namespace std;
typedef long long int ll;
typedef long double ld;
ll ESP=1e18;
ll FCB=1e9+7;
ll dir[][2]={{0,1},{0,-1},{1,0},{-1,0}};

vector<ll>vv;

void fac(ll n)
{
 for(ll i=1;i*i<=n;i++)
 {
  if(n%i==0)
  {
   if(n/i==i)
    vv.pb(i);
   else
    vv.pb(i),vv.pb(n/i);
  }
 }
}

int main()
{
 hs;
 ll t;
 cin>>t;
 while(t--)
 {
  ll n,k;
  cin>>n>>k;
  string str;
  cin>>str;
  map<char,ll>mp;
  set<char>ss;
  rep(i,0,n)
  {
    ss.insert(str[i]);
    mp[str[i]]++;
  }
  if(ss.size()==1)
    cout<<n<<"\n";
  else
  {
   fac(k);
   ll zz=vv.size();
   ll ans=0;
   for(ll i=0;i<zz;i++)
   {
   ll cnt=0,minn=10000;
   for(auto xx:mp)
   {
    if(xx.ss>=vv[i])
        minn=min(minn,xx.ss),cnt++;
   }
   if(k>n && cnt!=0)
   {
   ll yy=cnt*vv[i];
   ll lo=lower_bound(vv.begin(),vv.end(),yy)-vv.begin();
   if(vv[lo]!=yy)
     lo--;
   cnt=vv[lo];
   }
    ans=max(ans,minn*cnt);
   }
   cout<<ans<<"\n";
  }
  vv.clear();
 }
 return 0;
}


#include<bits/stdc++.h>
#define mem(dp,a) memset(dp,a,sizeof(dp))
#define pb(x) push_back(x)
#define m_p(x,y) make_pair(x,y)
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define per(i,b,a) for (ll i=b;i>=a;i--)
#define all(v) (v).begin(),(v).end()
#define ff first
#define ss second
#define pi acosl(-1.0l)
#define hs ios_base::sync_with_stdio(false);cin.tie(NULL);
#define fixed(n) fixed<<setprecision(n)
using namespace std;
typedef long long int ll;
typedef long double ld;
ll ESP=1e18;
ll FCB=1e9+7;
ll dir[][2]={{0,1},{0,-1},{1,0},{-1,0}};

vector<ll>vv;

void fac(ll n)
{
 for(ll i=1;i*i<=n;i++)
 {
  if(n%i==0)
  {
   if(n/i==i)
    vv.pb(i);
   else
    vv.pb(i),vv.pb(n/i);
  }
 }
}

int main()
{
 hs;
 ll t;
 cin>>t;
 while(t--)
 {
  ll n,k;
  cin>>n>>k;
  string str;
  cin>>str;
  map<char,ll>mp;
  set<char>ss;
  rep(i,0,n)
  {
    ss.insert(str[i]);
    mp[str[i]]++;
  }
  if(ss.size()==1)
    cout<<n<<"\n";
  else
  {
   fac(k);
   ll zz=vv.size();
   ll ans=0;
   for(ll i=0;i<zz;i++)
   {
   ll cnt=0;
   for(auto xx:mp)
   {
    if(xx.ss>=vv[i])
        cnt++;
   }
   if(k>n && cnt!=0)
   {
   ll yy=cnt*vv[i];
   ll lo=lower_bound(vv.begin(),vv.end(),yy)-vv.begin();
   if(vv[lo]!=yy)
     lo--;
   cnt=vv[lo];
   }
    ans=max(ans,cnt*vv[i]);
   }
   ll pp=n/ans;
   if(pp>=1)
    ans*=pp;
   cout<<ans<<"\n";
  }
  vv.clear();

 }
 return 0;
}


#include<bits/stdc++.h>
#define mem(dp,a) memset(dp,a,sizeof(dp))
#define pb(x) push_back(x)
#define m_p(x,y) make_pair(x,y)
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define per(i,b,a) for (ll i=b;i>=a;i--)
#define all(v) (v).begin(),(v).end()
#define ff first
#define ss second
#define pi acosl(-1.0l)
#define hs ios_base::sync_with_stdio(false);cin.tie(NULL);
#define fixed(n) fixed<<setprecision(n)
using namespace std;
typedef long long int ll;
typedef long double ld;
ll ESP=1e18;
ll FCB=1e9+7;
ll dir[][2]={{0,1},{0,-1},{1,0},{-1,0}};

int main()
{
 hs;
 ll t;
 cin>>t;
 while(t--)
 {
  ll n,k;
  cin>>n>>k;
  string str;
  cin>>str;
  map<char,ll>mp;
  set<char>ss;
  rep(i,0,n)
  {
    ss.insert(str[i]);
    mp[str[i]]++;
  }
  if(ss.size()==1)
    cout<<n<<"\n";
  else
  {
   ll ans=0;
   for(ll len=1;len<=n;len++)
   {
    ll blocks=__gcd(len,k);
    ll per_blocks=len/blocks;
    ll cnt=0;
    for(auto xx:mp)
      cnt+=xx.ss/per_blocks;

    if(cnt>=blocks)
        ans=len;

   }
   cout<<ans<<"\n";
  }
 }
 return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<set>
#include<string>
#include<queue>
using namespace std;
const int maxn = 3000;
int num[30];
set<int> s;

int gcd(int a, int b)
{
	return (b == 0) ? a : gcd(b, a % b);
}

int main()
{
	ios::sync_with_stdio(false);
	int T; cin >> T;
	while (T--)
	{
		memset(num, 0, sizeof(num));
		int N; int K; cin >> N >> K;
		for (int i = 1; i <= N; i++)
		{
			char c; cin >> c; num[c - 'a'] += 1;
		}

		for (int n = N; n >= 1; n--)
		{
			int k = gcd(n, K);
			int t = n / k;
			int sum = 0;
			for (int i = 0; i < 26; i++) sum += num[i] / t * t;
			if (sum >= n)
			{
				cout << n << endl; break;
			}
		}
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<set>
#include<string>
#include<queue>
using namespace std;
const int maxn = 3000;
int num[30];
set<int> s;

int gcd(int a, int b)
{
	return (b == 0) ? a : gcd(b, a % b);
}

int a[maxn];
bool check(int K, int N)
{
	K = min(K, N);
	int sum = 0; int t = N / K;
	//if (t == 0) return false;
	for (auto it = s.begin(); it != s.end(); it++)
	{
		sum += (num[*it] / t) * t;
	}
	return sum >= N;
}

int main()
{
	ios::sync_with_stdio(false);
	int T; cin >> T;
	while (T--)
	{
		memset(num, 0, sizeof(num)); s.clear();
		int N; cin >> N; int K; cin >> K;
		for (int i = 1; i <= N; i++)
		{
			char c; cin >> c; s.insert(c - 'a'); num[c - 'a'] += 1;
		}

		if (K >= N || N / K == 1)
		{
			for (int i = K; i >= 1; i--)
			{
				if (K % i == 0 && i <= N) { K = i; break; }
			}
			cout << K << endl; continue;
		}

		int ans = K;
		for (int j = 1; j <= K; j++)
		{
			if (gcd(K, j) == j)
			{
				for (int i = j; i <= N; i += j)
				{
					if (check(j, i) == true)ans = max(ans, i);
					else break;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define N 100
int read(){
	int w=0,f=1;
	char c=' ';
	while(c<'0'||c>'9')c=getchar(),f=c=='-'?-1:f;
	while(c>='0'&&c<='9')w=w*10+c-48,c=getchar();
	return w*f;
}
int t,n,k,len,ans,num[30],a[N],cnt;
string s;
signed main(){
	t=read();
	while(t--){
		n=read(),k=read(),ans=1,cnt=0;
	    cin>>s;
	    len=s.length();
	    memset(num,0,sizeof(num));
	    for(int i=0;i<len;i++)
			num[s[i]-'a']++,ans=max(ans,num[s[i]-'a']);
	    for(int i=2;i<=k;i++)
	        if(k%i==0)a[++cnt]=i;
	    for(int i=1;i<=cnt;i++)
	    	for(int j=1;j<=n/a[i];j++){
	    		bool flag=0;
	    		int x=a[i];
	    		for(int l=0;l<26;l++){
			        x-=num[l]/j;
			        if(x<=0){
			        	flag=1;
			        	break;
					}
			    }
			    if(flag)ans=max(ans,a[i]*j);
			}
	    printf("%lld\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define N 100
int read(){
	int w=0,f=1;
	char c=' ';
	while(c<'0'||c>'9')c=getchar(),f=c=='-'?-1:f;
	while(c>='0'&&c<='9')w=w*10+c-48,c=getchar();
	return w*f;
}
int t,n,k,len,ans,num[30],a[N],cnt;
string s;
signed main(){
	t=read();
	while(t--){
		n=read(),k=read(),ans=1,cnt=0;
	    cin>>s;
	    len=s.length();
	    memset(num,0,sizeof(num));
	    for(int i=0;i<len;i++)
			num[s[i]-'a']++,ans=max(ans,num[s[i]-'a']);
	    for(int i=2;i<=k;i++)
	        if(k%i==0)a[++cnt]=i;
	    for(int i=1;i<=cnt;i++)
	    	for(int j=1;j<=n/a[i];j++){
	    		bool flag=0;
	    		int x=a[i];
	    		for(int l=0;l<25;l++){
			        x-=num[l]/j;
			        if(x<=0){
			        	flag=1;
			        	break;
					}
			    }
			    if(flag)ans=max(ans,a[i]*j);
			}
	    printf("%lld\n",ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define FAST_INPUT ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long

using namespace std;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

void solve() {
	int n, k;
	cin >> n >> k;
	string s;
	cin >> s;

	vector<int> cnt(26);
	for (char c : s) {
		cnt[c - 'a']++;
	}

	for (int len = n; len >= 1; len--) {
		vector<bool> used(len);
		vector<int> cycles;

		for (int i = 0; i < len; i++) {
			debug(i);
			if (used[i]) {
				continue;
			}
			int j = (i + k) % len;
			used[i] = true;
			cycles.push_back(0);
			cycles.back()++;
			debug(len, j, cycles);

			while (!used[j]) {
				cycles.back()++;
				used[j] = true;
				j = (j + k) % len;
				debug(j, cycles);
			}
		}

		debug(cycles);

		vector<int> cur_cnt(cnt);
		sort(cycles.begin(), cycles.end());
		sort(cur_cnt.begin(), cur_cnt.end());

		bool can_fill = true;

		while (!cycles.empty()) {
			if (cur_cnt.back() < cycles.back()) {
				can_fill = false;
				break;
			} else {
				cur_cnt.back() -= cycles.back();
				cycles.pop_back();
				sort(cur_cnt.begin(), cur_cnt.end());
			}
		}

		if (can_fill) {
			cout << len << endl;
			return;
		}
	}

}

int main() {
    FAST_INPUT;

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>
#define FAST_INPUT ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long

using namespace std;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

void solve() {
	int n, k;
	cin >> n >> k;
	string s;
	cin >> s;

	vector<int> cnt(26);
	for (char c : s) {
		cnt[c - 'a']++;
	}

	for (int len = n; len >= 1; len--) {
		vector<bool> used(len);
		vector<int> cycles;

		for (int i = 0; i < len; i++) {
			if (used[i]) {
				continue;
			}
			int j = (i + k) % len;
			used[i] = true;
			cycles.push_back(0);
			cycles.back()++;

			while (!used[j]) {
				cycles.back()++;
				used[j] = true;
				j = (j + k) % len;
			}
		}

		debug(cycles);

		vector<int> cur_cnt(cnt);
		sort(cycles.begin(), cycles.end());
		sort(cur_cnt.begin(), cur_cnt.end());

		bool can_fill = true;

		while (!cycles.empty()) {
			if (cur_cnt.back() < cycles.back()) {
				can_fill = false;
				break;
			} else {
				cur_cnt.back() -= cycles.back();
				cycles.pop_back();
				sort(cur_cnt.begin(), cur_cnt.end());
			}
		}

		if (can_fill) {
			cout << len << endl;
			return;
		}
	}

}

int main() {
    FAST_INPUT;

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,t,k;
string c;
int ch[27],s[2010];
int main(){
    cin>>t;
    while(t--){
            memset(ch,0,sizeof ch);
        cin>>n>>k;
        cin>>c;
        for(int i=0;i<n;i++){
            int a=c[i]-97;
            ch[a]++;
        }
      /*  for(int i=0;i<26;i++){
            if(ch[i])
            cout<<i<<" "<<ch[i]<<endl;
        }*/
        int x=k,cnt=0;;
        for(int i=1;i<=k/i;i++){
        if(k%i==0)
        {
            s[cnt++]=i;
            if(i!=k/i)s[cnt++]=k/i;
        }
      }
      sort(s,s+cnt);
      /*for(int i=0;i<cnt;i++)
        cout<<s[i]<<endl;*/
   /* if(n<=k){
        for(int i=cnt-1;i>=0;i--){
            if(s[i]<=n){
                cout<<s[i]<<endl;
                break;
            }
        }
    }
    else{*/
            int ans=0;
        for(int i=0;i<cnt;i++){
            for(int j=1;j*s[i]<=n;j++){
                int ant=0;
               // cout<<"s[i]  j"<<s[i]<<" "<<j<<endl;
                for(int k=0;k<26;k++){
                    ant+=ch[k]/j;
                }
                if(ant>=s[i])
                ans=max(ans,s[i]*j);
                else break;
            }
        }
        cout<<ans<<endl;
    //}
    }
    return 0;
}
//  aaebdbabdbbddaadaadc   j周期次数 s[i]周期大小
/*
10
200 548
xyvvmyfhfomsdnqltzhktyoayrzlqwhcbtbaznvudtxymwhsrlfgfxmztckkqsshnmqikzjxwuxwksofjfosotbeekzdxyljomnnvfzuzdyvhaakadmonnskegiqnfkisyynootpvmkblwfdpfcidaomayzhywxhhivvvtujxkygmtkfqsbjopfddidfqgjykzfncfeb
200 559
luzrgywfemprizaxktszmugkfdakyjfdwxrfcjouklnxjzypgdlxkyplaornwpktkedqimlvzpcaivmtavwskvelctnvyziytxudzxudfueyukgagjpwkltpfmowhcwjdfxzzjziuaegmwzsisthasicflftxfkfukywxndsdfdbjwhncmjfmbevlkybpxesyrgnssow

*/


#include<bits/stdc++.h>
using namespace std;
int color[26];
int getCnt(int n)
{
	int ans=0; for(int c:color) ans+=c/n*n;
	return ans;
} // OK
void test()
{
	int n,k; cin>>n>>k;
	for(int &c:color) c=0;
	string s; cin>>s;
	for(char c:s) color[c-'a']++;
	int ans=0;
	for(int j=1;j<=n;j++)
	{
		int k1=__gcd(j,k);
		if(getCnt(j/k1)>=j) ans=j;
	}
	cout<<ans<<'\n';
} // OK
int main()
{
	iostream::sync_with_stdio(false);
	int q; cin>>q;
	while(q--) test();
}


#include <iostream>
#include <cassert>
#include <algorithm>
#include <string>
using namespace std;

int main(int argc,char ** argv)
{
    int t;
    cin>>t;
    while(t--){
        int n,k,t,tm[27]={0},ans=0;
        string s;
        cin>>n>>k>>s;
        for(int i=0;i<s.length();++i){
            ++tm[s[i]-'a'];
        }
        sort(tm,tm+26);
        int l=0,r=26;
        while(!tm[l])++l;
        for(int lps=1;lps<=tm[r-1];++lps){
            int ca=0;
            for(int i=l;i<r;++i){
                ca+=tm[i]/lps;
                if(tm[i]/lps==0)++l;
            }
            while(k%ca!=0)--ca;
            ans=max(ans,ca*lps);
        }

        cout<<ans<<endl;
    }

    return 0;
}


#include <iostream>
#include <cassert>
#include <algorithm>
#include <string>
using namespace std;

int main(int argc,char ** argv)
{
    int t;
    cin>>t;
    while(t--){
        int n,k,t,tm[27]={0},ans=0;
        string s;
        cin>>n>>k>>s;
        for(int i=0;i<s.length();++i){
            ++tm[s[i]-'a'];
        }
        sort(tm,tm+26);
        for(int i=1;i<=26&&tm[26-i]!=0;++i){
            if(k%i==0||k%tm[26-i]==0)
                ans=max(ans,tm[26-i]*i);
        }

        if(k>=s.length()){
            t=s.length();
            while(k%t!=0)--t;
            ans=max(t,ans);
        }
        cout<<ans<<endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e4+7;

int cnt[30];
int t;
char s[3000];
int gcd(int a,int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int main (){
    cin>>t;
    while(t--){
        memset(cnt,0,sizeof(cnt));
        int n,k,ans;
        cin>>n>>k;
        cin>>s;
        for(int i=0;i<n;i++){
            int x=s[i]-'a';
            cnt[x]++;
        }
        for(int i=n;i>=3;i--){
            int x=gcd(i,k),cc=0;
            //printf("***%d %d\n",x,cc);
            int xx=i/x;
            for(int i=0;i<26;i++){
                cc+=cnt[i]/xx;
            }
            if(cc>=x){
                ans=i;
                break;
            }

        }
        printf ("%d\n",ans);

    }

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e4+7;

int cnt[30];
int t;
char s[3000];
int gcd(int a,int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int main (){
    cin>>t;
    while(t--){
        memset(cnt,0,sizeof(cnt));
        int n,k,ans=1;
        cin>>n>>k;
        cin>>s;
        for(int i=0;i<n;i++){
            int x=s[i]-'a';
            cnt[x]++;
        }
        for(int i=n;i>=1;i--){
            int x=gcd(i,k),cc=0;
            //printf("***%d %d\n",x,cc);
            int xx=i/x;
            for(int i=0;i<26;i++){
                cc+=cnt[i]/xx;
            }
            if(cc>=x){
                ans=i;
                break;
            }
        }
        printf ("%d\n",ans);
    }
}


#include<bits/stdc++.h>
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define mem0(x) memset(x,0,sizeof(x))
#define meminf(x) memset(x,0x3f,sizeof(x))
#define VI vector<int>
#define VL vector<ll>
#define pb push_back

using namespace  std;

const int N = 150;
int num[26],idx[26];
bool cmp(int a,int b){
    return num[a]>num[b];
}

int check(int numm,int len){
    int cnt = 0;
    rep(i,0,25){
        cnt+=num[i]/numm;
        if(cnt>=len) return 1;
    }
    return 0;
}
int main(){
    ios::sync_with_stdio(0);
    int t;cin>>t;

    while(t--){
        int n,k;cin>>n>>k;
        string s;cin>>s;
        int kind = 0;
        for(auto &it:s){
            if(num[it-'a']==0){
                kind++;
            }
            ++num[it-'a'];
        }
        rep(i,0,25){
            idx[i] = i;
        }
        sort(idx,idx+26,cmp);
        //int lim = sqrt(k+0.5);
        int ans = num[idx[0]];
        rep(i,2,k){
            int tans = 0;
            if(k%i==0){
                int l = 1,r = n/i;
                while(l<=r){
                    int mid = (l+r)>>1;
                    if(check(mid,i)){
                        l = mid+1;
                        tans = mid;
                    }else{
                        r = mid-1;
                    }
                }

            }
            ans = max(ans,tans*i);
            //cout<<i<<":"<<ans<<endl;
        }
        cout<<ans<<endl;

        mem0(num);
    }
}


#include<bits/stdc++.h>
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define mem0(x) memset(x,0,sizeof(x))
#define meminf(x) memset(x,0x3f,sizeof(x))
#define VI vector<int>
#define VL vector<ll>
#define pb push_back

using namespace  std;

const int N = 150;
int num[26],idx[26];
bool cmp(int a,int b){
    return num[a]>num[b];
}

int check(int numm,int len){
    int cnt = 0;
    rep(i,0,25){
        cnt+=num[i]/numm;
        if(cnt>=len) return 1;
    }
    return 0;
}
int main(){
    ios::sync_with_stdio(0);
    int t;cin>>t;

    while(t--){
        int n,k;cin>>n>>k;
        string s;cin>>s;
        int kind = 0;
        for(auto &it:s){
            if(num[it-'a']==0){
                kind++;
            }
            ++num[it-'a'];
        }
        rep(i,0,25){
            idx[i] = i;
        }
        sort(idx,idx+26,cmp);
        //int lim = sqrt(k+0.5);
        int ans = 0;
        rep(i,2,k){
            int tans = 0;
            if(k%i==0){
                int l = 1,r = n/i;
                while(l<=r){
                    int mid = (l+r)>>1;
                    if(check(mid,i)){
                        l = mid+1;
                        tans = mid;
                    }else{
                        r = mid-1;
                    }
                }

            }
            ans = max(ans,tans*i);
            //cout<<i<<":"<<ans<<endl;
        }
        cout<<ans<<endl;

        mem0(num);
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,t,k;
string c;
int ch[27],s[2010];
int main(){
    cin>>t;
    while(t--){
            memset(ch,0,sizeof ch);
        cin>>n>>k;
        cin>>c;
        for(int i=0;i<n;i++){
            int a=c[i]-97;
            ch[a]++;
        }
       /* for(int i=0;i<26;i++){
            if(ch[i])
            cout<<i<<" "<<ch[i]<<endl;
        }*/
        int x=k,cnt=0;;
        for(int i=1;i<=k/i;i++){
        if(k%i==0)
        {
            s[cnt++]=i;
            if(i!=k/i)s[cnt++]=k/i;
        }
      }
      sort(s,s+cnt);
      /*for(int i=0;i<cnt;i++)
        cout<<s[i]<<endl;*/
    if(n<=k){
        for(int i=cnt-1;i>=0;i--){
            if(s[i]<=n){
                cout<<s[i]<<endl;
                break;
            }
        }
    }
    else{
            int ans=0;
        for(int i=0;i<cnt;i++){
            for(int j=1;j*s[i]<=n;j++){
                int ant=0;
                for(int k=0;k<26;k++){
                    ant+=ch[k]/j;
                }
                if(ant>=s[i])
                ans=max(ans,s[i]*j);
                else break;
            }
        }
        cout<<ans<<endl;
    }
    }
    return 0;
}
//  aaebdbabdbbddaadaadc   j周期次数 s[i]周期大小


#include <bits/stdc++.h>
#define int long long

using namespace std;

const int O = 1e6 + 5;

int n, t, k, cnt[1000];

string s;

main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> t;
    while (t --){
        memset(cnt, 0, sizeof(cnt));
        cin >> n >> k >> s;
        s = " " + s;
        for (int i = 1; i <= n; ++ i) ++ cnt[s[i]];
        int ans = 0;
        for (int i = 1; i <= k; ++ i){
            if (k % i == 0){
                for (int j = 1; j <= n / i; ++ j){
                    int cur = 0;
                    for (int z = 'a'; z <= 'z'; ++ z){
                        cur += (cnt[z] / j);
                    }
                    if (cur >= i) ans = max(ans, j * i);
                }
            }
        }
        cout << ans << '\n';
    }
}


#include <bits/stdc++.h>
#define int long long

using namespace std;

const int O = 1e6 + 5;

int n, t, k, cnt[1000];

string s;

main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> t;
    while (t --){
        memset(cnt, 0, sizeof(cnt));
        cin >> n >> k >> s;
        s = " " + s;
        for (int i = 1; i <= n; ++ i) ++ cnt[s[i]];
        int ans = 0;
        for (int i = 1; i <= k; ++ i){
            if (k % i == 0){
                for (int j = 1; j <= n; ++ j){
                    int cur = 0;
                    for (int z = 'a'; z <= 'z'; ++ z){
                        cur += (cnt[z] / j);
                    }
                    if (cur >= i) ans = max(ans, j * i);
                }
            }
        }
        cout << ans << '\n';
    }
}


#include <bits/stdc++.h>
#define int long long

using namespace std;

const int O = 1e6 + 5;

int n, t, k, cnt[1000];

string s;

main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> t;
    while (t --){
        memset(cnt, 0, sizeof(cnt));
        cin >> n >> k >> s;
        s = " " + s;
        for (int i = 1; i <= n; ++ i) ++ cnt[s[i]];
        int ans = 0;
        for (int i = 2; i <= k; ++ i){
            if (k % i == 0){
                for (int j = 1; j <= n; ++ j){
                    int cur = 0;
                    for (int z = 'a'; z <= 'z'; ++ z){
                        cur += (cnt[z] / j);
                    }
                    if (cur >= i) ans = max(ans, j * i);
                }
            }
        }
        cout << ans << '\n';
    }
}


#include <bits/stdc++.h>
#define int long long

using namespace std;

const int O = 1e6 + 5;

int n, t, k, cnt[1000];

string s;

main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> t;
    while (t --){
        memset(cnt, 0, sizeof(cnt));
        cin >> n >> k >> s;
        s = " " + s;
        int maxx = 0;
        for (int i = 1; i <= n; ++ i){
            ++ cnt[s[i]];
            maxx = max(maxx, cnt[s[i]]);
        }
        if (k == 1){
            cout << maxx << '\n';
            continue;
        }
        int ans = 0;
        for (int i = 2; i <= k; ++ i){
            if (k % i == 0){
                for (int j = 1; j <= n; ++ j){
                    int cur = 0;
                    for (int z = 'a'; z <= 'z'; ++ z){
                        cur += (cnt[z] / j);
                    }
                    if (cur >= i) ans = max(ans, j * i);
                }
            }
        }
        cout << ans << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

int n;

bool can(int m, int k, int counts[]) {
    int totalChars = 0;
    for(int i = 0; i < 26; i++) {
        totalChars += counts[i]/m;
    }
    return totalChars >= k;
}

int findBest(int k, int counts[]) {
    int l = 0, r = n/k;
    while (l < r) {
        int m = (l+r+1)/2;
        if (can(m, k, counts)) {
            l = m;
        } else {
            r = m-1;
        }
    }
    return l * k;
}

void solve() {
    int k;
    cin >> n >> k;
    string s;
    cin >> s;
    int counts[26] = {0};
    for(int i = 0; i < n; i++) {
        counts[s[i]-'a']++;
    }
    sort(counts, counts + 26, greater<int>());

    int bestLen = 0;
    for(int i = 1; i <= k; i++) {
        if (k%i == 0) {
            bestLen = max(bestLen, findBest(i, counts));
        }
    }
    cout << bestLen << endl;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int t;
    cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

int n;

bool can(int m, int k, int counts[]) {
    int totalChars = 0;
    for(int i = 0; i < 26; i++) {
        totalChars += counts[i]/m;
    }
    return totalChars >= k;
}

int findBest(int k, int counts[]) {
    int l = 0, r = n/k;
    while (l < r) {
        int m = (l+r)/2;
        if (can(m, k, counts)) {
            l = m;
        } else {
            r = m-1;
        }
    }
    return l * k;
}

void solve() {
    int k;
    cin >> n >> k;
    string s;
    cin >> s;
    int counts[26] = {0};
    for(int i = 0; i < n; i++) {
        counts[s[i]-'a']++;
    }
    sort(counts, counts + 26, greater<int>());

    int bestLen = 0;
    for(int i = 1; i <= k; i++) {
        if (k%i == 0) {
            bestLen = max(bestLen, findBest(i, counts));
        }
    }
    cout << bestLen << endl;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int t;
    cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=2e3+5;
char s[maxn];
int n,k,a[26];
bool jg(int tot,int div){
	for(int i=0;i<26;i++){
		tot-=a[i]/div;
	}
	return tot<=0;
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&k);
		scanf("%s",s);
		memset(a,0,sizeof(a));
		for(int i=0;i<n;i++){
			a[s[i]-'a']++;
		}
		int ans=0;
		for(int i=1;i<=k;i++){
			if(k%i==0){
				int l=1,r=n,mid;
				while(r-l>1){
					mid=(l+r)>>1;
					if(jg(i,mid)){
						l=mid;
					}else{
						r=mid-1;
					}
				}
//				cout<<i<<" "<<l<<" "<<r<<endl;
				if(jg(i,r)){
					ans=max(ans,i*r);
				}else
				if(jg(i,l)){
					ans=max(ans,i*l);
				}
			}
		}
		printf("%d\n",ans);
	}
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxm=2e6+5;
char s[maxm];
int f[maxm];
int a[maxm];
int b[maxm];
int n,k;
map<int,int>mark;
int check(int x,int mid){//循环节长度,循环次数
    int cnt=0;
    for(auto i:mark){
        cnt+=i.second/mid;
    }
    return cnt>=x;
}
void solve(){
    cin>>n>>k;
    scanf("%s",s+1);
    mark.clear();
    for(int i=1;i<=n;i++){
        mark[(int)s[i]]++;
    }
    int res=0;
    for(int i=1;i<=k&&i<=n;i++){//枚举因子
        if(k%i)continue;
        int l=1,r=n/i;//枚举循环次数,循环次数*i可以小于k
        int ans=0;
        while(l<=r){
            int mid=(l+r)/2;
            if(check(i,mid))ans=mid,l=mid+1;
            else r=mid-1;
        }
        if(ans!=-1){
            res=max(res,ans*i);
        }
    }
    cout<<res<<endl;
}
signed main(){
    int T=1;
    cin>>T;
    while(T--){
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxm=2e6+5;
char s[maxm];
int f[maxm];
int a[maxm];
int b[maxm];
int n,k;
map<int,int>mark;
int check(int x,int mid){//循环节长度,循环次数
    int cnt=0;
    for(auto i:mark){
        cnt+=i.second/mid;
    }
    return cnt>=x;
}
void solve(){
    cin>>n>>k;
    scanf("%s",s+1);
    mark.clear();
    for(int i=1;i<=n;i++){
        mark[(int)s[i]]++;
    }
    int res=0;
    for(int i=1;i<=k&&i<=n;i++){//枚举因子
        if(k%i)continue;
        int l=min(n,k)/i,r=max(n,k)/i;//枚举循环次数
        int ans=0;
        while(l<=r){
            int mid=(l+r)/2;
            if(check(i,mid))ans=mid,l=mid+1;
            else r=mid-1;
        }
        if(ans!=-1){
            res=max(res,ans*i);
        }
    }
    cout<<res<<endl;
}
signed main(){
    int T=1;
    cin>>T;
    while(T--){
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxm=2e6+5;
char s[maxm];
int f[maxm];
int a[maxm];
int b[maxm];
int n,k;
map<int,int>mark;
int check(int x,int mid){//循环节长度,循环次数
    int cnt=0;
    for(auto i:mark){
        cnt+=i.second/mid;
    }
    return cnt>=x;
}
void solve(){
    cin>>n>>k;
    scanf("%s",s+1);
    mark.clear();
    for(int i=1;i<=n;i++){
        mark[(int)s[i]]++;
    }
    int res=0;
    for(int i=1;i<=k&&i<=n;i++){//枚举因子
        if(k%i)continue;
        int l=1,r=n/i;//枚举循环次数
        int ans=0;
        while(l<=r){
            int mid=(l+r)/2;
            if(check(i,mid))ans=mid,l=mid+1;
            else r=mid-1;
        }
        if(ans!=-1){
            res=max(res,ans*i);
        }
    }
    cout<<res<<endl;
}
signed main(){
    int T=1;
    cin>>T;
    while(T--){
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ull unsigned long long
#define md 1000000007
#define pb push_back
#define ff first
#define ss second
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
using namespace __gnu_pbds;
typedef priority_queue<int, vector<int>, greater<int> > min_pq;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
    cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}

ll power(ll x, ll y, ll m)
{
    if (y == 0)
        return 1;
    ll p = power(x, y / 2, m) % m;
    p = (p * p) % m;

    return (y % 2 == 0) ? p : ((x % m) * p) % m;
}

ll epower(ll x, ll y)
{
    if (y == 0)
        return 1;
    ll p = epower(x, y / 2);
    p = (p * p);

    return (y % 2 == 0) ? p : (x * p);
}

ll modInverse(ll a, ll m)
{
    return power(a, m - 2, m);
}

// #######################################CODE!!#####################################################
int ch[26];
set<int>factors;
void solve() {
    memset(ch, 0, sizeof(ch));
    factors.clear();
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    for (auto &it : s) {
        ch[it - 'a']++;
    }
    sort(ch, ch + 26);
    reverse(ch, ch + 26);
    for (int i = 1; i <= sqrt(k); ++i) {
        if (k % i == 0) {
            factors.insert(i);
            factors.insert(k / i);
        }
    }
    int ans = 0;
    for (auto &it : factors) {
        for (int i = it; i <= n; i += it) {
            int total = i, d = it;
            int nd = total / d;
            bool flag = false;
            for (int j = 0; j < 26; ++j) {
                int res = ch[j] / nd;
                total -= min(d, res) * nd;
                d -= min(d, res);
                if (d == 0)
                {
                    flag = true;
                    break;
                }
            }
            if (flag)
                ans = max(ans, i);
        }
    }
    cout << ans << endl;
}

int main() {
    fio();
    int t;
    cin >> t;
    //t = 1;
    while (t--) {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ull unsigned long long
#define md 1000000007
#define pb push_back
#define ff first
#define ss second
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
using namespace __gnu_pbds;
typedef priority_queue<int, vector<int>, greater<int> > min_pq;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
    cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}

ll power(ll x, ll y, ll m)
{
    if (y == 0)
        return 1;
    ll p = power(x, y / 2, m) % m;
    p = (p * p) % m;

    return (y % 2 == 0) ? p : ((x % m) * p) % m;
}

ll epower(ll x, ll y)
{
    if (y == 0)
        return 1;
    ll p = epower(x, y / 2);
    p = (p * p);

    return (y % 2 == 0) ? p : (x * p);
}

ll modInverse(ll a, ll m)
{
    return power(a, m - 2, m);
}

// #######################################CODE!!#####################################################
int ch[26];
vector<int>a;
set<int>factors;
void solve() {
    memset(ch, 0, sizeof(ch));
    factors.clear();
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    for (auto &it : s) {
        ch[it - 'a']++;
    }
    sort(ch, ch + 26);
    reverse(ch, ch + 26);
    for (int i = 1; i < sqrt(k); ++i) {
        if (k % i == 0) {
            factors.insert(i);
            factors.insert(k / i);
        }
    }
    int ans = 0;
    for (auto &it : factors) {
        for (int i = it; i <= n; i += it) {
            int total = i, d = it;
            int nd = total / d;
            bool flag = false;
            for (int j = 0; j < 26; ++j) {
                int res = ch[j] / nd;
                total -= min(d, res) * nd;
                d -= min(d, res);
                if (d == 0)
                {
                    flag = true;
                    break;
                }
            }
            if (flag)
                ans = max(ans, i);
        }
    }
    cout << ans << endl;
}

int main() {
    fio();
    int t;
    cin >> t;
    //t = 1;
    while (t--) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
int cnt[30];
char a[2005];
int main(){
    int t,n,k;
    scanf("%d",&t);
    while(t--){
        memset(cnt,0,sizeof(cnt));
        scanf("%d %d",&n,&k);
        scanf("%s",a);
        for(int i=0;i<n;++i)
            ++cnt[a[i]-'a'];
        int ans=0;
        for(int i=1;i<=n;++i){
            int tmp=0,j=i/__gcd(i,k);
            for(int l=0;l<26;++l) tmp+=cnt[l]/j;
            if(tmp>=__gcd(i,k))
                ans=max(ans,i);
        }
        printf("%d\n",ans);
    }
}


#include <bits/stdc++.h>

using namespace std;
int cnt[30];
char a[2005];
int main(){
    int t,n,k;
    scanf("%d",&t);
    while(t--){
        memset(cnt,0,sizeof(cnt));
        scanf("%d %d",&n,&k);
        scanf("%s",a);
        for(int i=0;i<n;++i)
            ++cnt[a[i]-'a'];
        int ans=0;
        if(k%n==0){
            printf("%d\n",n);
            continue;
        }
        for(int i=1;i<=k&&i<=n;++i){
            if(k%i==0){
                ans=max(ans,i);
            }
        }
        for(int i=1;i<=n;++i){
            if(__gcd(i,k)!=1){
                int tmp=0,j=i/__gcd(i,k);
                for(int l=0;l<26;++l) tmp+=cnt[l]/j;
                if(tmp>=__gcd(i,k))
                    ans=max(ans,i);
            }
        }
        printf("%d\n",ans);
    }
}


#include <bits/stdc++.h>

using namespace std;
int cnt[30];
char a[2005];
int main(){
    int t,n,k;
    scanf("%d",&t);
    while(t--){
        memset(cnt,0,sizeof(cnt));
        scanf("%d %d",&n,&k);
        scanf("%s",a);
        for(int i=0;i<n;++i)
            ++cnt[a[i]-'a'];
        int ans=0;
        for(int i=1;i<=n;++i){

                int tmp=0,j=i/__gcd(i,k);
                for(int l=0;l<26;++l) tmp+=cnt[l]/j;
                if(tmp>=__gcd(i,k))
                    ans=max(ans,i);

        }
        printf("%d\n",ans);
    }
}


#include <bits/stdc++.h>

using namespace std;
int cnt[30];
char a[2005];
int main(){
    int t,n,k;
    scanf("%d",&t);
    while(t--){
        memset(cnt,0,sizeof(cnt));
        scanf("%d %d",&n,&k);
        scanf("%s",a);
        for(int i=0;i<n;++i)
            ++cnt[a[i]-'a'];
        int ans=0;
        for(int i=1;i<=n;++i){
            if(__gcd(i,k)!=1){
                int tmp=0,j=i/__gcd(i,k);
                for(int l=0;l<26;++l) tmp+=cnt[l]/j;
                if(tmp>=__gcd(i,k))
                    ans=max(ans,i);
            }
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		int n, k;
		cin >> n >> k;
		string zodis;
		cin >> zodis;
		int simboliai['z'+1];
		for(int i='a'; i<='z'; i++)
			simboliai[i] = 0;
		for(int i=0; i<n; i++)
			simboliai[zodis[i]]++;
		int ats = 1;
		for(int i='a'; i<='z'; i++)
			ats = max(ats, simboliai[i]);
		for(int i=1; i<=n; i++)
		{
			if(k%i==0)
				ats = max(ats, i);
		}
		int sudedu = n;

		for(int i=1; i<=n; i++)
		{
			sudedu = 0;
			for(int j='a'; j<='z'; j++)
			{
				sudedu += simboliai[j] / i;
			}
			if(sudedu>=k)
				ats = max(i*k, ats);
			//cout << "gavau: " << i << " " << sudedu << endl;
		}
		cout << ats << "\n";
	}
	return 0;
}


/*input
6
6 3
abcbac
3 6
aaa
7 1000
abczgyo
5 4
ababa
20 10
aaebdbabdbbddaadaadc
20 5
ecbedececacbcbccbdec
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		int n, k;
		cin >> n >> k;
		string zodis;
		cin >> zodis;
		int simboliai['z'+1];
		for(int i='a'; i<='z'; i++)
			simboliai[i] = 0;
		for(int i=0; i<n; i++)
			simboliai[zodis[i]]++;
		int ats = 1;
		for(int i='a'; i<='z'; i++)
			ats = max(ats, simboliai[i]);
		for(int i=1; i<=n; i++)
		{
			if(k%i==0)
				ats = max(ats, i);
		}
		int sudedu;
		int kuris;
		for(int dal=1; dal*dal<=k; dal++)
		{
			if(k%dal!=0)
				continue;
			for(int i=1; i<=n; i++)
			{
				sudedu = 0;
				for(int j='a'; j<='z'; j++)
				{
					sudedu += simboliai[j] / i;
				}
				if(sudedu>=dal)
				{
					if(i*dal>ats)
						kuris = dal;
					ats = max(i*dal, ats);
				}

				//cout << "gavau: " << i << " " << sudedu << endl;
			}
			dal = k / dal;
			for(int i=1; i<=n; i++)
			{
				sudedu = 0;
				for(int j='a'; j<='z'; j++)
				{
					sudedu += simboliai[j] / i;
				}
				if(sudedu>=dal)
				{
					if(i*dal>ats)
						kuris = dal;
					ats = max(i*dal, ats);
				}
				//cout << "gavau: " << i << " " << sudedu << endl;
			}
			dal = k / dal;
		}
		cout << ats <<  "\n";
	}
	return 0;
}


/*input
6
6 3
abcbac
3 6
aaa
7 1000
abczgyo
5 4
ababa
20 10
aaebdbabdbbddaadaadc
20 5
ecbedececacbcbccbdec
*/

// Nihesh Anderson - knandy

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef long double ld;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; // find_by_order, order_of_key
ll MOD=1000000007;

#define endl '\n'
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define pb push_back
ll INF = 2000000000000000001;
long double EPS = 1e-9;
ll power(ll a, ll n, ll md){if(n==0){return 1;}else{int res=power(a,n/2,md);res=(res*res)%md;if(n%2!=0){res=(res*a)%md;}return res;}}
random_device rndm;
mt19937 grndm(rndm());
void mix(int* a, int* b){shuffle(a,b,grndm);}

int main(){
	fast_cin();
	// freopen("input.in","r",stdin);
	// freopen("output.out","w",stdout);
	int t; cin >> t;
	while(t--) {
		int n, km; cin >> n >> km;

		string s; cin >> s;
		int f[26];
		memset(f, 0, sizeof(f));
		for(auto c: s) {
			f[c - 'a']++;
		}
		sort(f, f + 26, greater<int>());
		int len = n;
		while(len >= 1) {
			int cnt = 0, period = __gcd(km, len), times = len / period;
			for(int i = 0; i < 26; i++) {
				cnt += f[i] / times;
			}
			if(cnt >= period) {
				break;
			}
			len--;
		}
		cout << len << endl;
	}
	return 0;
}


/*input
6
6 3
abcbac
3 6
aaa
7 1000
abczgyo
5 4
ababa
20 10
aaebdbabdbbddaadaadc
20 5
ecbedececacbcbccbdec
*/

// Nihesh Anderson - knandy

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef long double ld;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; // find_by_order, order_of_key
ll MOD=1000000007;

#define endl '\n'
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define pb push_back
ll INF = 2000000000000000001;
long double EPS = 1e-9;
ll power(ll a, ll n, ll md){if(n==0){return 1;}else{int res=power(a,n/2,md);res=(res*res)%md;if(n%2!=0){res=(res*a)%md;}return res;}}
random_device rndm;
mt19937 grndm(rndm());
void mix(int* a, int* b){shuffle(a,b,grndm);}

int main(){
	fast_cin();
	// freopen("input.in","r",stdin);
	// freopen("output.out","w",stdout);
	int t; cin >> t;
	while(t--) {
		int n, km; cin >> n >> km;

		string s; cin >> s;
		int f[26];
		memset(f, 0, sizeof(f));
		for(auto c: s) {
			f[c - 'a']++;
		}
		sort(f, f + 26, greater<int>());
		int len = 2000;
		while(len >= 1) {
			int k = km % len;
			if(k == 0 && len > n) {
				len--;
				continue;
			}
			else if(k == 0) {
				break;
			}
			if(len % k != 0) {
				len--;
				continue;
			}
			int cnt = 0, times = len / k;
			for(int i = 0; i < 26; i++) {
				cnt += f[i] / times;
			}
			if(cnt >= k) {
				break;
			}
			len--;
		}
		cout << len << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n, k, cnt[26];
char s[2010];
int main() {
    int T;
    cin >> T;
    while(T--) {
        scanf("%d%d", &n, &k);
        scanf("%s", s);
        for (int i = 0; i < 26; ++i) cnt[i] = 0;
        for (int i = 0; i < n; ++i) {
            ++cnt[s[i] - 'a'];
        }
        int ans = 0;
        for (int i = 1; i <= k; ++i) {
            if (k % i) continue;
            for (int j = 1; i * j <= n; ++j) {
                int len = 0;
                for (int k = 0; k < 26; ++k) {
                    len += cnt[k] / j;
                }

                if (len >= i) {
                    //cout << i << " " << j << " " << len << endl;
                    ans = max(ans, i * j);
                }
            }
        }
        printf("%d\n", ans);
    }
}


#include <bits/stdc++.h>
using namespace std;

const int a = 26;
int t, n, k;
string s;

int gdc(int a, int b){
	if(b==0) return a;
	return gdc(b,a%b);
}

int main(){
	cin >> t;
	while(t--){
		cin >> n >> k >> s;
		vector <int> d(a,0);
		for(auto ch:s) d[ch-'a']++;
		for(int i=n;i>0;i--){
			int g=gdc(i,k);
			int ans=0, c = i/g;
			for(int j=0;j<a;j++) ans+=d[j]/c;
			if(ans>=g){
				cout << i << endl;
				break;
			}
		}

	}
}


#include <bits/stdc++.h>
using namespace std;

const int a = 26;
int t, n, k;
string s;

int gdc(int a, int b){
	if(b==0) return a;
	return gdc(b,a%b);
}

int main(){
	cin >> t;
	while(t--){

		cin >> n >> k >> s;
		vector <int> d(a,0);
		for(auto ch:s) d[ch-'a']++;
		for(int i=n;i>0;i--){
			int g=gdc(i,k);
			int ans=0, c = i/g;
			for(int j=0;j<a;j++) ans+=d[j]/c;
			if(ans>=g){
				cout << i << endl;
				break;
			}
		}

	}
}


#include <bits/stdc++.h>
using namespace std;
#define inputarr(a,n) for(ll i=0;i<n;i++) cin>>a[i];
#define prllarr(a,n) for(ll i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl;
#define pb push_back
#define ll long long
#define mod 1000000007
#define foi ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0);
#define in(n) scanf("%lld",&n);
#define in2(x,y) scanf("%lld %lld",&(x),&(y));
#define in3(x,y,z) scanf("%lld %lld %lld",&(x),&(y),&(z));
#define in4(x,y,z,m) scanf("%lld %lld %lld %lld",&(x),&(y),&(z),&(m));
#define out(n) printf("%lld\n",n);
#define out2(x,y) printf("%lld %lld\n",x,y);
#define test(t)  ll t; in(t);while(t--)
#define set(arr,n,s) for(ll i=0;i<n;i++){arr[i]=s;}
#define fr(i,s,e) for(ll i=s;i<e;i++)
#define frr(i,s,e) for(ll i=e-1;i>=s;i--)
#define vi vector<ll>
#define see2(x,y) cout<<x<<" "<<y<<endl;
#define see(x) cout<<x<<endl;
#define see3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;
#define see4(x,y,z,m) cout<<x<<" "<<y<<" "<<z<<" "<<m<<endl;

ll power(ll x,ll y,ll p){ll res=1;x=x%p;while(y>0){if(y&1){res=(res*x)%p;} y=y>>1;x=(x*x)%p;}return res;}
ll modInverse(ll a,ll p){return power(a,p-2,p);}//used with feemat little//only for prime
void gcd(ll a, ll b, ll& d, ll& u, ll& v) {
    if (b == 0) {
        d = a;
        u = 1;
        v = 0;
    } else {
        gcd(b, a % b, d, v, u);
        // a u + b v = b u' + (a - (a/b) * b) v'
        // = a v' + b (u' - (a/b) v').
        v -= a / b * u;
    }
}
ll Inverse(ll a, ll m) {
    a %= m;
    if (a < 0) a += m;
    ll d, u, v;
    gcd(a, m, d, u, v);
    assert(d == 1);
    u %= m;
    if (u < 0) u += m;
    return u;
}

ll gcd(ll x,ll y){if(x==0 || y==0){return max(y,x);}return gcd(y%x,x);}
ll gcdExtended(ll a,ll b,ll &x,ll &y){if(a==0){x=0;y=1;return b;}ll x1,y1;ll gcd = gcdExtended(b%a,a,x1,y1);x=y1-(b/a)*x1;y=x1;return gcd;}//o(log(b))
;
ll n,k;

ll poss(vector<ll> & a ,ll len){
    ll kd=k%len;
    ll numcycle=gcd(len,kd);
    ll onecycle=len/numcycle;
    for(auto p:a){
        numcycle-=(p/onecycle);
    }
    return numcycle<=0;

}
int main()
{

    // char z='a'+0;
    // cout<<z;
    test(t){
        in2(n,k)
        string s;
        cin>>s;
        ll freq[26]={0};
        for(ll i=0;i<s.size();i++){
            ll x=s[i]-'a';
            freq[x]++;
        }
        vi a;
        for(ll i=0;i<=25;i++){
            if(freq[i]>0)a.pb(freq[i]);
        }
        ll l=0;ll r=s.size();
        ll ans=0;
        for(ll i=1;i<=n;i++){
            if(poss(a,i)){
                ans=i;
            }
        }
        out(ans);

    }

}
/*error-----
convert every int to long long eg-1LL
create array with proper analysis of problem constrain
check mod also
*/


#include <bits/stdc++.h>
using namespace std;
#define inputarr(a,n) for(ll i=0;i<n;i++) cin>>a[i];
#define prllarr(a,n) for(ll i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl;
#define pb push_back
#define ll long long
#define mod 1000000007
#define foi ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0);
#define in(n) scanf("%lld",&n);
#define in2(x,y) scanf("%lld %lld",&(x),&(y));
#define in3(x,y,z) scanf("%lld %lld %lld",&(x),&(y),&(z));
#define in4(x,y,z,m) scanf("%lld %lld %lld %lld",&(x),&(y),&(z),&(m));
#define out(n) printf("%lld\n",n);
#define out2(x,y) printf("%lld %lld\n",x,y);
#define test(t)  ll t; in(t);while(t--)
#define set(arr,n,s) for(ll i=0;i<n;i++){arr[i]=s;}
#define fr(i,s,e) for(ll i=s;i<e;i++)
#define frr(i,s,e) for(ll i=e-1;i>=s;i--)
#define vi vector<ll>
#define see2(x,y) cout<<x<<" "<<y<<endl;
#define see(x) cout<<x<<endl;
#define see3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;
#define see4(x,y,z,m) cout<<x<<" "<<y<<" "<<z<<" "<<m<<endl;

ll power(ll x,ll y,ll p){ll res=1;x=x%p;while(y>0){if(y&1){res=(res*x)%p;} y=y>>1;x=(x*x)%p;}return res;}
ll modInverse(ll a,ll p){return power(a,p-2,p);}//used with feemat little//only for prime
void gcd(ll a, ll b, ll& d, ll& u, ll& v) {
    if (b == 0) {
        d = a;
        u = 1;
        v = 0;
    } else {
        gcd(b, a % b, d, v, u);
        // a u + b v = b u' + (a - (a/b) * b) v'
        // = a v' + b (u' - (a/b) v').
        v -= a / b * u;
    }
}
ll Inverse(ll a, ll m) {
    a %= m;
    if (a < 0) a += m;
    ll d, u, v;
    gcd(a, m, d, u, v);
    assert(d == 1);
    u %= m;
    if (u < 0) u += m;
    return u;
}

ll gcd(ll x,ll y){if(x==0 || y==0){return max(y,x);}return gcd(y%x,x);}
ll gcdExtended(ll a,ll b,ll &x,ll &y){if(a==0){x=0;y=1;return b;}ll x1,y1;ll gcd = gcdExtended(b%a,a,x1,y1);x=y1-(b/a)*x1;y=x1;return gcd;}//o(log(b))
;
ll n,k;

ll poss(vector<ll> & a ,ll len){
    ll numcycle=gcd(len,k);
    ll onecycle=len/numcycle;
    for(auto p:a){
        numcycle-=(p/onecycle);
    }
    return numcycle<=0;

}
int main()
{

    // char z='a'+0;
    // cout<<z;
    test(t){
        in2(n,k)
        string s;
        cin>>s;
        ll freq[26]={0};
        for(ll i=0;i<s.size();i++){
            ll x=s[i]-'a';
            freq[x]++;
        }
        vi a;
        for(ll i=0;i<=25;i++){
            if(freq[i]>0)a.pb(freq[i]);
        }
        ll l=0;ll r=s.size();
        ll ans=0;
        for(ll i=1;i<=n;i++){
            if(poss(a,i)){
                ans=i;
            }
        }
        out(ans);

    }

}
/*error-----
convert every int to long long eg-1LL
create array with proper analysis of problem constrain
check mod also
*/


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;

const int N = 2006;
int n, k;
int num[N];
char a[N];
bool cmp(int a, int b) {return a > b;}

int temp[N], ans;
void dfs(int u, int now, int mx) {
	if(now == mx) {

		int mi = 3000;
		for(int i = 1; i < u; i++) {
			int x = num[i] / temp[i];
			mi = min(mi, x);
		}
		ans = max(ans, mi*mx);

		return;
	}
	//字母 数量 总量
	for(int i = 1; i <= mx-now && i <= temp[u-1]; i++) {
		temp[u] = i;
		dfs(u+1, now+i, mx);
		temp[u] = 0;
	}
}

void work() {
	cin >> n >> k;
	scanf("%s", a + 1);

	memset(num, 0, sizeof(num));
	for(int i = 1; i <= n; i++) num[a[i]-'a'+1]++;
	sort(num+1, num+1+26, cmp);

	ans = 0;
	temp[0] = 3000;
	for(int i = 1; i <= k && i <= n; i++) {
	//枚举循环元
		if(k % i) continue;

		//找到循环节可能的组合
		dfs(1, 0, i);
	}
	cout << ans << endl;
}

int main() {
	int q; cin >> q;
	while(q--)
		work();

	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;

const int N = 2006;
int n, k;
int num[N];
char a[N];
bool cmp(int a, int b) {return a > b;}

void work() {
	cin >> n >> k;
	scanf("%s", a + 1);

	memset(num, 0, sizeof(num));
	for(int i = 1; i <= n; i++) num[a[i]-'a'+1]++;

	int ans = 0;

	for(int i = 1; i <= k; i++) {
	//枚举循环元
		if(k % i) continue;
		for(int j = 1; j * i <= n; j++) {
			int sum = 0;
			for(int k = 1; k <= 26; k++)
				sum += num[k] / j;
			if(sum >= i) ans = max(ans, i*j);
		}
	}
	cout << ans << endl;
}

int main() {
	int q; cin >> q;
	while(q--)
		work();

	return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <cstring>
using namespace std;

int a[26];
int main ()
{
    int t;
    cin >> t;

    while(t --)
    {
        int n, k;

        cin >> n >> k;
        string s;
        cin >> s;
        memset(a, 0, sizeof(a));
        for (int i = 0; i < s.size(); i ++) a[s[i] - 'a'] ++;
        vector<int> v;
        for (int i = 1; i <= sqrt(k); i ++)
            if (k % i == 0)
            {
                if(i <= n) v.push_back(i);
                if(k / i <= n) v.push_back(k / i);
            }

        int sum = 0;
        for (int i = 0; i < v.size(); i ++)
        {
            int l = 1, r = n;
            while(l < r)
            {
                int mid = (l + r + 1) >> 1;
                int num = 0;
                for (int i = 0; i < 26; i ++)
                    if(a[i] != 0)
                    {

                        num += a[i] / mid;
                    }
                if(num >= v[i]) l = mid;
                else r = mid - 1;

            }

            if(l * v[i] > sum ) sum = l * v[i];
        }
        cout << sum <<endl;

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define MAXN 2000005

int n,k;
char s[MAXN];
int mp[26];
int ans;

bool check(int x) {
    int tt=x%k;
    int t=x/k;
    int cnt=0,re=0;
    for(int i=0;i<25;i++){
        cnt+=mp[i]/t;
        re+=mp[i]%t;
    }
    if(cnt>=t && re>=tt) return true;
    else return false;
}

void solve() {
    memset(mp,0,sizeof(mp));

    scanf("%d %d", &n, &k);
    scanf("%s",s);
    int len=strlen(s);
    set<pair<int,char>>
    for(int i=0;i<len;i++){
        mp[s[i]-'a']++;

    }
    int l=1,r=n;
    while(l<r){
        int mid=(l+r+1)>>1;
        if(check(mid)) l=mid;
        else r=mid-1;
    }
    printf("%d\n", l);
}

int main() {
    int T=1;
    scanf("%d", &T);
    while(T--) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define MAXN 2000005

int n,k;
char s[MAXN];
int mp[26];
int ans;

bool check(int x) {
    int tt=x%k;
    int t=x/k;
    int cnt=0,re=0;
    int tot=0;
    for(int i=0;i<25;i++){
        if(t){
            int count=mp[i]/(t+1);
            if(count<=tt) tt-=count;
            else tt=0,count=tt;
            cnt+=(mp[i]-count*(k+1))/t;
        }
        tot+=mp[i];
    }
    if(t==0){
        return tot>=x;
    }
    if(cnt>=k && tt==0) return true;
    else return false;
}

void solve() {
    memset(mp,0,sizeof(mp));
    scanf("%d %d", &n, &k);
    scanf("%s",s);
    int len=strlen(s);
    for(int i=0;i<len;i++){
        mp[s[i]-'a']++;

    }
    int l=1,r=n;
    while(l<r){
        int mid=(l+r+1)>>1;
        if(check(mid)) l=mid;
        else r=mid-1;
    }
    printf("%d\n", l);
}

int main() {
    int T=1;
    scanf("%d", &T);
    while(T--) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define MAXN 2000005

int n,k;
char s[MAXN];
int mp[26];
int ans;

bool check(int x) {
    int t=x/k;
    int cnt=0,tot=0;
    for(int i=0;i<26;i++){
        if(t){
            cnt+=mp[i]/t;
        }
        tot+=mp[i];
    }
    if(t==0){
        return tot>=x;
    }
    if(cnt>=k) return true;
    else return false;
}

void solve() {
    memset(mp,0,sizeof(mp));
    scanf("%d %d", &n, &k);
    scanf("%s",s);
    int len=strlen(s);
    for(int i=0;i<len;i++){
        mp[s[i]-'a']++;

    }
    if(k>=n){
        int ans=0;
        for(int i=2;i*i<=k;i++){
            if(k%i==0){
                while(k%i==0)k/=i;
                if(i<=n) ans=i;
            }
        }
        if(k<=n && k>1)ans=k;
        printf("%d\n", ans);
        return;
    }
    int ans=0;

    for(int i=1;i*i<=k;i++) {
        if(k%i==0){
            int t=k;
            k=i;
            int cnt;
            for(cnt=n/k*k;cnt>=1;cnt-=k){
                if(check(cnt))break;
            }
            ans=max(ans,cnt);
            // printf("fuck %d:%d\n", k,cnt);
            k=t/i;
            for(cnt=n/k*k;cnt>=1;cnt-=k){
                if(check(cnt))break;
            }
            ans=max(ans,cnt);
            // printf("fuck %d:%d\n", k,cnt);
            k=t;
        }
    }
    printf("%d\n", ans);

}

int main() {
    int T=1;
    scanf("%d", &T);
    while(T--) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define MAXN 2000005

int n,k;
char s[MAXN];
int mp[26];
int ans;

bool check(int x) {
    int tt=x%k;
    int t=x/k;
    int cnt=0,re=0;
    int tot=0;
    for(int i=0;i<25;i++){
        if(t){
            cnt+=mp[i]/t;
            re+=mp[i]%t;
        }
        tot+=mp[i];
    }
    if(t==0){
        return tot>=x;
    }
    if(cnt>=t && re>=tt) return true;
    else return false;
}

void solve() {
    memset(mp,0,sizeof(mp));
    scanf("%d %d", &n, &k);
    scanf("%s",s);
    int len=strlen(s);
    set<pair<int,char>> st;
    for(int i=0;i<len;i++){
        mp[s[i]-'a']++;

    }
    int l=1,r=n;
    while(l<r){
        int mid=(l+r+1)>>1;
        if(check(mid)) l=mid;
        else r=mid-1;
    }
    printf("%d\n", l);
}

int main() {
    int T=1;
    scanf("%d", &T);
    while(T--) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define MAXN 2000005

int n,k;
char s[MAXN];
int mp[26];
int ans;

bool check(int x) {
    int t=x/k;
    int cnt=0,tot=0;
    for(int i=0;i<26;i++){
        if(t){
            cnt+=mp[i]/t;
        }
        tot+=mp[i];
    }
    if(t==0){
        return tot>=x;
    }
    if(cnt>=k) return true;
    else return false;
}

void solve() {
    memset(mp,0,sizeof(mp));
    scanf("%d %d", &n, &k);
    scanf("%s",s);
    int len=strlen(s);
    for(int i=0;i<len;i++){
        mp[s[i]-'a']++;

    }
    int ans=0;
    for(int i=1;i*i<=k;i++) {
        if(k%i==0){
            int t=k;
            k=i;
            int cnt;
            for(cnt=n/k*k;cnt>=1;cnt-=k){
                if(check(cnt))break;
            }
            ans=max(ans,cnt);
            // printf("fuck %d:%d\n", k,cnt);
            k=t/i;
            for(cnt=n/k*k;cnt>=1;cnt-=k){
                if(check(cnt))break;
            }
            ans=max(ans,cnt);
            // printf("fuck %d:%d\n", k,cnt);
            k=t;
        }
    }
    printf("%d\n", ans);

}

int main() {
    int T=1;
    scanf("%d", &T);
    while(T--) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define MAXN 2000005

int n,k;
char s[MAXN];
int mp[26];
int ans;

bool check(int x) {
    int tt=x%k;
    int t=x/k;
    int cnt=0,re=0;
    int tot=0;
    for(int i=0;i<25;i++){
        if(t){
            int count=mp[i]/(t+1);
            if(count<=tt) tt-=count;
            else tt=0,count=tt;
            cnt+=(mp[i]-count*(t+1))/t;
        }
        tot+=mp[i];
    }
    if(t==0){
        return tot>=x;
    }
    if(cnt>=k && tt==0) return true;
    else return false;
}

void solve() {
    memset(mp,0,sizeof(mp));
    scanf("%d %d", &n, &k);
    scanf("%s",s);
    int len=strlen(s);
    for(int i=0;i<len;i++){
        mp[s[i]-'a']++;

    }
    int l=1,r=n;
    while(l<r){
        int mid=(l+r+1)>>1;
        if(check(mid)) l=mid;
        else r=mid-1;
    }
    printf("%d\n", l);
}

int main() {
    int T=1;
    scanf("%d", &T);
    while(T--) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define MAXN 2000005

int n,k;
char s[MAXN];
int mp[26];
int ans;

bool check(int x) {
    int tt=x%k;
    int t=x/k;
    int cnt=0,re=0;
    for(int i=0;i<25;i++){
        cnt+=mp[i]/t;
        re+=mp[i]%t;
    }
    if(cnt>=t && re>=tt) return true;
    else return false;
}

void solve() {
    memset(mp,0,sizeof(mp));

    scanf("%d %d", &n, &k);
    scanf("%s",s);
    int len=strlen(s);
    set<pair<int,char>> st;
    for(int i=0;i<len;i++){
        mp[s[i]-'a']++;

    }
    int l=1,r=n;
    while(l<r){
        int mid=(l+r+1)>>1;
        if(check(mid)) l=mid;
        else r=mid-1;
    }
    printf("%d\n", l);
}

int main() {
    int T=1;
    scanf("%d", &T);
    while(T--) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define MAXN 2000005

int n,k;
char s[MAXN];
int mp[26];
int ans;

void check(int x) {
    int tt=x%k;
    int t=x/k;
    int cnt=0,re=0;
    for(int i=0;i<25;i++){
        cnt+=mp[i]/t;
        re+=mp[i]%t;
    }
    if(cnt>=t && re>=tt)return true;
    else return false;
}

void solve() {
    memset(mp,0,sizeof(mp));

    scanf("%d %d", &n, &k);
    scanf("%s",s);
    int len=strlen(s);
    set<pair<int,char>>
    for(int i=0;i<len;i++){
        mp[s[i]-'a']++;

    }
    int l=1,r=n;
    while(l<r){
        int mid=(l+r+1)>>1;
        if(check(mid)) l=mid;
        else r=mid-1;
    }
    printf("%d\n", l);
}

int main() {
    int T=1;
    scanf("%d", &T);
    while(T--) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define MAXN 2000005

int n,k;
char s[MAXN];
int mp[26];
int ans;

bool check(int x) {
    int tt=x%k;
    int t=x/k;
    int cnt=0,re=0;
    int tot=0;
    for(int i=0;i<25;i++){
        if(t){
            cnt+=mp[i]/t;
            re+=mp[i]%t;
        }
        tot+=mp[i];
    }
    if(t==0){
        return tot>=x;
    }
    if(cnt>=k && re>=tt) return true;
    else return false;
}

void solve() {
    memset(mp,0,sizeof(mp));
    scanf("%d %d", &n, &k);
    scanf("%s",s);
    int len=strlen(s);
    set<pair<int,char>> st;
    for(int i=0;i<len;i++){
        mp[s[i]-'a']++;

    }
    int l=1,r=n;
    while(l<r){
        int mid=(l+r+1)>>1;
        if(check(mid)) l=mid;
        else r=mid-1;
    }
    printf("%d\n", l);
}

int main() {
    int T=1;
    scanf("%d", &T);
    while(T--) {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include <iomanip>
#include<math.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define fi first
#define se second
#define gap ' '
#define abs(x) ((x)>0?(x):-(x))
#define INF 1e18
#define mod 1000000007
#define inf 1e9
#define pi 3.14159265358979
ll powr(ll m,ll n)
{
    if(m==1)
        return 1;
    if(n==0)
        return 1;
    if(n==1)
        return m%mod;
    if(n%2)
    {
        ll te=powr(m,n/2);
        return (((te*te)%mod)*(m%mod))%mod;
    }
    else
    {
        ll te=powr(m,n/2);
        return (te*te)%mod;
    }
}
int par[2005];
int Find(int x)
{
    if(par[x]==x)
        return x;
    return par[x]=Find(par[x]);
}
void Union(int a,int b)
{
    par[Find(b)]=Find(a);
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int tc=1;
    cin>>tc;
    int cs=0;

    while(tc--)
    {
        int n,k;
        cin>>n>>k;
        string s;
        cin>>s;
        int bead[26]= {0};
        for(int i=0; i<n; i++)
            bead[s[i]-'a']++;

        vector<int> v;
        for(int i=0; i<26; i++)
            if(bead[i])
                v.pb(bead[i]);

        sort(v.begin(),v.end());

        for(int i=0; i<=n; i++)
            par[i]=i;

        for(int i=n; i>0; i--)
        {
            for(int j=0; j<i; j++)
            {
                int sm=(j+k)%i;
                if(Find(sm)!=Find(j))
                    Union(sm,j);
            }

            int cmp[i+5]= {0};
            vector<int> a;
            vector<int> cpy;
            for(int i=0;i<v.size();i++)
                cpy.pb(v[i]);

            for(int j=0; j<i; j++)
                cmp[Find(j)]++;

            for(int j=0; j<i; j++)
                if(cmp[j])
                    {a.pb(cmp[j]);
                    //cout<<cmp[j]<<endl;
                    }

            sort(a.begin(),a.end());
            int l=0,f=1;

            for(int i=0; i<a.size(); i++)
            {
                while(l<cpy.size() && cpy[l]<a[i])
                    l++;
                if(l==v.size())
                {
                    f=0;
                    break;
                }
                cpy[l]-=a[i];
            }
            if(f)
            {
                cout<<i<<endl;
                break;
            }

            for(int i=0; i<=n; i++)
            par[i]=i;

        }

    }

}


#include<bits/stdc++.h>
#define faster_io ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define mem(a,b) memset(a, b, sizeof a)
#define F first
#define S second
#define ll long long
#define pb push_back
#define pll pair<ll, ll>
using namespace std;
const ll N = 1e6 + 1e5, inf = 3e18, mod = 1e9 + 7;
ll tc, n, m, q, a, b, c, d, i, j, k, ans;
ll A[N], fr[30], flag[100];
string s;

int main()
{
    faster_io;
    cin >> tc;
    while(tc--)
    {
        mem(fr, 0);
        cin >> n >> m >> s;
        for(i = 0; i < n; i++)
            fr[s[i] - 'a']++;
        vector <ll> vec;
        for(i = 0; i < 26; i++)
            vec.pb(fr[i]);
        sort(vec.rbegin(), vec.rend());
        ll ans = 0;
        for(i = 1; i <= m; i++)
        {
            if(m % i)
                continue;
            for(j = 1; j <= 2000; j++)
            {
                ll cnt = 0;
                for(k = 0; k < vec.size() && vec[k] >= j; k++) cnt += vec[k] / j;
                //cout << i << ' ' << j << ' ' << cnt << endl;
                if(cnt < i)
                    break;
                ans = max(ans, j * i);
            }
        }
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>
#define faster_io ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define mem(a,b) memset(a, b, sizeof a)
#define F first
#define S second
#define ll long long
#define pb push_back
#define pll pair<ll, ll>
using namespace std;
const ll N = 1e6 + 1e5, inf = 3e18, mod = 1e9 + 7;
ll tc, n, m, q, a, b, c, d, i, j, k, ans;
ll A[N], fr[30], flag[100];
string s;

int main()
{
    faster_io;
    cin >> tc;
    while(tc--)
    {
        mem(fr, 0);
        cin >> n >> m >> s;
        for(i = 0; i < n; i++)
            fr[s[i] - 'a']++;
        vector <ll> vec;
        for(i = 0; i < 26; i++)
            vec.pb(fr[i]);
        sort(vec.rbegin(), vec.rend());
        ll ans = 0;
        for(i = 1; i <= m; i++)
        {
            if(m % i)
                continue;
            for(j = min((ll)vec.size(), i); j > 0; j--)
            {
                ll mn = 3000;
                for(k = 0; k < j; k++)
                {
                    a = i / j;
                    if(k < (i % j))
                        a++;
                    mn = min(mn, vec[k] / a);
                }
                ans = max(ans, mn * i);
            }
        }
        cout << ans << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define FAST ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);srand(time(NULL))
#define ll long long int
#define pb push_back
#define INF LONG_LONG_MAX
#define mod 1000000007
#define mp make_pair
#define inf 1e18+5
#define ff first
#define ss second
const int N = 1e5 + 10;
bool pri[1000001];
ll c[1001][1001];
/*void binomialCoeff()
{
    //ll C[n + 1][k + 1];
    ll i, j;
    for (i = 0; i <= 1000; i++)
    {
        for (j = 0; j <= min(i, 1000); j++)
        {
            if (j == 0 || j == i)
                C[i][j] = 1;
            else
                C[i][j] = C[i - 1][j - 1] +
                          C[i - 1][j];
        }
    }
    //return C[n][k];
}*/
void sieve()
{
    ll i,j;
    for(i=2;i*i<=1000000;i++)
    {
        if(pri[i])
        {
            for(j=i*i;j<=1000000;j+=i)
                pri[j]=0;
        }
    }
}
ll po(ll x,ll y,ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}
ll GCD(ll A, ll B) {
    if(B==0)
        return A;
    else
        return GCD(B, A % B);
}
int main()
{
    FAST;
    ll t,i,j,m;
    cin>>t;
    while(t--)
    {
        ll n,k;
        cin>>n>>k;
        string s;
        cin>>s;
        vector<ll> f;
        for(i=1;i*i<=k;i++)
        {
            if(k%i==0)
            {
                f.pb(i);
                if(k/i!=i)
                    f.pb(k/i);
            }
        }
        sort(f.begin(),f.end());
        if(n<=k)
        {
            for(i=f.size()-1;i>=0;i--)
            {
                if(f[i]<=n)
                {
                    cout<<f[i]<<endl;
                    break;
                }
            }
        }
        else
        {
            ll c[123]={0};
            for(i=0;i<n;i++)
                ++c[s[i]];
            ll ans=k;
            for(i=k;i<=n;i++)
            {
                for(j=0;j<f.size();j++)
                {
                    if(i%f[j]==0)
                    {
                        ll d=i/f[j];
                        ll an=0;
                        for(m=97;m<=122;m++)
                            an+=c[m]/d;
                        if(an*d>=i)
                            ans=max(ans,i);
                    }
                }
            }
            cout<<ans<<endl;
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
#define FAST ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);srand(time(NULL))
#define ll long long int
#define pb push_back
#define INF LONG_LONG_MAX
#define mod 1000000007
#define mp make_pair
#define inf 1e18+5
#define ff first
#define ss second
const int N = 1e5 + 10;
bool pri[1000001];
ll c[1001][1001];
/*void binomialCoeff()
{
    //ll C[n + 1][k + 1];
    ll i, j;
    for (i = 0; i <= 1000; i++)
    {
        for (j = 0; j <= min(i, 1000); j++)
        {
            if (j == 0 || j == i)
                C[i][j] = 1;
            else
                C[i][j] = C[i - 1][j - 1] +
                          C[i - 1][j];
        }
    }
    //return C[n][k];
}*/
void sieve()
{
    ll i,j;
    for(i=2;i*i<=1000000;i++)
    {
        if(pri[i])
        {
            for(j=i*i;j<=1000000;j+=i)
                pri[j]=0;
        }
    }
}
ll po(ll x,ll y,ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}
ll GCD(ll A, ll B) {
    if(B==0)
        return A;
    else
        return GCD(B, A % B);
}
int main()
{
    FAST;
    ll t,i,j,m;
    cin>>t;
    while(t--)
    {
        ll n,k;
        cin>>n>>k;
        string s;
        cin>>s;
        vector<ll> f;
        for(i=1;i*i<=k;i++)
        {
            if(k%i==0)
            {
                f.pb(i);
                if(k/i!=i)
                    f.pb(k/i);
            }
        }
        sort(f.begin(),f.end());
        //for(i=0;i<f.size();i++)
          //  cout<<f[i]<<" ";
        //cout<<endl;
        if(n<=k)
        {
            ll ans=1;
            ll c[123]={0};
            for(i=0;i<n;i++)
                ++c[s[i]];
            for(i=1;i<=n;i++)
            {
                for(j=0;j<f.size();j++)
                {
                    if(f[j]>i)
                        break;
                    if(i%f[j]==0)
                    {
                        //if(i==5)
                            //cout<<f[j]<<endl;
                        ll d=i/f[j];
                        ll an=0;
                        for(m=97;m<=122;m++)
                            an+=(c[m]/d);
                        //if(i==5)
                          //  cout<<d<<endl;
                        if(an*d>=i)
                        {
                            ans=max(ans,i);
                            break;
                        }
                    }
                }
            }
            //cout<<"HI"<<endl;
            cout<<ans<<endl;
        }
        else
        {
            ll c[123]={0};
            for(i=0;i<n;i++)
                ++c[s[i]];
            ll ans=k;
            for(i=k;i<=n;i++)
            {
                for(j=0;j<f.size();j++)
                {
                    if(i%f[j]==0)
                    {
                        ll d=i/f[j];
                        ll an=0;
                        for(m=97;m<=122;m++)
                            an+=c[m]/d;
                        if(an*d>=i)
                            ans=max(ans,i);
                    }
                }
            }
            //cout<<"HI"<<endl;
            cout<<ans<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;

#define int long long
#define PB push_back

inline int read() {
	int x=0,y=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') {
		if(ch=='-') y=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9') {
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*y;
}

const int N=2000;

int t,a,b,p[26];
char s[N+10];
vector<int> st;

inline bool check(int n,int m) {
    for(int i=0;i<=25;i++) {
        n-=p[i]/m;
        if(n<=0) return true;
    }
    return false;
}

signed main() {
	// freopen("in1.in","r",stdin);
    t=read();
    while(t--) {
        a=read();
        b=read();
        scanf("%s",s+1);
        int len=strlen(s+1);
        memset(p,0,sizeof(p));
        st.clear();
        int ans=0;
        for(int i=1;i<=len;i++) p[s[i]-'a']++,ans=max(ans,p[s[i]-'a']);
        for(int i=2;i<=b;i++)
            if(b%i==0) st.PB(i);
        for(int i=0;i<st.size();i++) {
            int n=st[i],l=1,r=a/n;
            while(l<=r) {
                int mid=(l+r)>>1;
                if(check(n,mid)) ans=max(ans,mid*n),l=mid+1;
                else r=mid-1;
            }
        }
        printf("%lld\n",ans);
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define int long long
#define PB push_back

inline int read() {
	int x=0,y=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') {
		if(ch=='-') y=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9') {
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*y;
}

const int N=2000;

int t,a,b,p[26];
char s[N+10];
vector<int> st;

inline bool check(int n,int m) {
    for(int i=0;i<=25;i++) {
        n-=p[i]/m;
        if(n<=0) return true;
    }
    return false;
}

signed main() {
	// freopen("in1.in","r",stdin);
    t=read();
    while(t--) {
        a=read();
        b=read();
        scanf("%s",s+1);
        int len=strlen(s+1);
        memset(p,0,sizeof(p));
        st.clear();
        for(int i=1;i<=len;i++) p[s[i]-'a']++;
        for(int i=2;i<=b;i++)
            if(b%i==0) st.PB(i);
        int ans=0;
        for(int i=0;i<st.size();i++) {
            int n=st[i],l=1,r=a/n;
            while(l<=r) {
                int mid=(l+r)>>1;
                if(check(n,mid)) ans=max(ans,mid*n),l=mid+1;
                else r=mid-1;
            }
        }
        printf("%lld\n",ans);
    }
	return 0;
}


/*
    we try with all posible sizes (n, n-1, , ... , 1)

    if we have a size m necklace, we have
    to identify all k-sized-jump cycles

    key idea: if there are going to be d=gcd(m, k) cycles
    of the same size

    all we have to proof is that the sequence
    0, 0+k mod m, 0+2k mod m,...
    visits the same numbers as the sequence
    0, d mod m, 2d mod m, ...

    this is only true if kx congruent to id modulo m has a solution on fixed i
    which is not difficult to proove

    then we just have to check if there are enough quantity of each letter
    to cover all d cycles
*/

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <vector>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stack>
#include <utility>
#include <queue>
#include <set>

using namespace std;

typedef int64_t ll;
typedef long double ld;
typedef pair<int,ll> pill;
typedef vector<pill> vill;

typedef pair<int,int> pii;
typedef vector<pii> vii;

typedef vector<int> vi;

#define MINF -1000000000000
#define MOD  1000000007

#define loop(i,a,b) for(int i = a; i < b; ++i)
#define loopi(i,a,b) for(int i = a; i <= b; ++i)
#define invl(i,a,b) for(int i = a; i > b; --i)
#define invli(i,a,b) for(int i = a; i >= b; --i)

const int maxN = 100005;

//gets the greatest common divisor of two numbers
int gcd(int a, int b){
    if(a == 0)
        return b;
    else
        return gcd(b%a, a);
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    ll T; cin>>T;
    loopi(t, 1, T){
        int n, k;
        string s;
        cin>>n>>k;
        cin>>s;

        int bucket[26];
        fill(bucket, bucket + 26, 0);

        for(char c : s)
            ++bucket[c-'a'];

        sort(bucket, bucket+26);

        int cycles, length, d, i;
        int m = n+1;
        bool canConstructNecklace = false;
        while(!canConstructNecklace){
            --m;
            d = gcd(m, k);

            cycles = d;
            length = m/d;
            i = 25;
            while(cycles > 0 && i >= 0 &&  bucket[i] >= length){
                cycles -= bucket[i]/length;
                --i;
            }

            canConstructNecklace = (cycles <= 0);
        }
        cout<<m<<"\n";

    }
}


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <vector>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stack>
#include <utility>
#include <queue>
#include <set>

using namespace std;

typedef int64_t ll;
typedef long double ld;
typedef pair<int,ll> pill;
typedef vector<pill> vill;

typedef pair<int,int> pii;
typedef vector<pii> vii;

typedef vector<int> vi;

#define MINF -1000000000000
#define MOD  1000000007

#define loop(i,a,b) for(int i = a; i < b; ++i)
#define loopi(i,a,b) for(int i = a; i <= b; ++i)
#define invl(i,a,b) for(int i = a; i > b; --i)
#define invli(i,a,b) for(int i = a; i >= b; --i)

const int maxN = 100005;

//gets the greatest common divisor of two numbers
int gcd(int a, int b){
    if(a == 0)
        return b;
    else
        return gcd(b%a, a);
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    ll T; cin>>T;
    loopi(t, 1, T){
        int n, k;
        string s;
        cin>>n>>k;
        cin>>s;

        int bucket[26];
        fill(bucket, bucket + 26, 0);

        for(char c : s)
            ++bucket[c-'a'];

        sort(bucket, bucket+26);

        int cycles, length, d, i;
        int m = n+1;
        bool canConstructNecklace = false;
        while(!canConstructNecklace){
            --m;
            d = gcd(m, k);

            cycles = d;
            length = m/d;
            i = 25;
            while(cycles > 0 && i >= 0 &&  bucket[i] >= length){
                cycles -= bucket[i]/length;
                --i;
            }

            canConstructNecklace = (cycles <= 0);
        }
        cout<<m<<"\n";

    }
}


#include <iostream>

using namespace std;
int v [2001];
int vc ['z' + 1];
string s;
int main()
{
    ios_base::sync_with_stdio (false);
    cin . tie (NULL);
    cout . tie (NULL);
    int t , n , k;
    cin >> t;
    while (t --){
        int ans = 1 , o = 0;
        cin >> n >> k;
        cin >> s;
        for(int i = 0 ; i < s . size () ; ++ i)
            ++ vc [(int)s [i]];
        if (k == 1){
            for(int i = 'a' ; i <= 'z' ; ++ i)
                ans = max (ans , vc [(int)i]);
            cout << ans << '\n';
        }
   /*     if (k > n){
            while (n){
                if (k % n == 0){
                    cout << n << '\n';
                    break;
                }
                -- n;
            }
        }
        else*/if (k != 1){
            int f = 1;
            bool p = false ;
            while (f * f <= k){
                if (k % f == 0){
                   // if (f > 1)
                        v [++ o] = f;
                    if (f * f != k)
                        v [++ o] = k / f;
                }
                ++ f;
            }
            for(int i = 1 ; i <= o ; ++ i){
                int x = v [i];
                int y = k / x;
                bool pp = true;
                p = true;
                for (int j = 1 ; pp && j * x <= n ; ++ j ){
                    int nr = 0;
                    for(int ij = 'a' ; ij <= 'z' ; ++ ij)
                        nr += vc [ij] / j;
                    if (nr >= x)
                        ans = max (ans , x * j);
                    else
                        pp = false;
                }
            }
            cout << ans << '\n';

        }
        for(int i = 'a' ; i <= 'z' ; ++ i)
            vc [i] = 0;
        for(int i = 1 ; i <= o ; ++ i)
            v [i] = 0;
        s . clear ();
    }
    return 0;
}


#include <iostream>

using namespace std;
int v [2001];
int vc ['z' + 1];
int main()
{
    ios_base::sync_with_stdio (false);
    cin . tie (NULL);
    cout . tie (NULL);
    int t , n , k;
    string s;
    cin >> t;
    while (t --){
        int ans = 1;
        cin >> n >> k;
        cin >> s;
        for(int i = 0 ; i < s . size () ; ++ i)
            ++ vc [(int)s [i]];
        if (k == 1){
            for(int i = 'a' ; i <= 'z' ; ++ i)
                ans = max (ans , vc [(int)i]);
            cout << ans << '\n';
            continue;
        }
        if (k > n){
            while (n){
                if (k % n == 0){
                    cout << n << '\n';
                    break;
                }
                -- n;
            }
        }
        else{
            int f = 1 , ans = 0 , o = 0;
            bool p = false ;
            while (f * f <= k){
                if (k % f == 0){
                    if (f > 1)
                        v [++ o] = f;
                    if (f * f != k)
                        v [++ o] = k / f;
                }
                ++ f;
            }
            for(int i = 1 ; i <= o ; ++ i){
                int x = v [i];
                int y = k / x;
                bool pp = true;
                p = true;
                for (int j = y ; pp && j * x <= n ; ++ j ){
                    int nr = 0;
                    for(int ij = 'a' ; ij <= 'z' ; ++ ij)
                        nr += vc [ij] / j;
                    if (nr >= x)
                        ans = max (ans , x * j);
                    else
                        pp = false;
                }
            }
        cout << ans << '\n';

        }
        for(int i = 'a' ; i <= 'z' ; ++ i)
            vc [i] = 0;
    }
    return 0;
}


#include <iostream>

using namespace std;
int v [2001];
int vc ['z' + 1];
string s;
int main()
{
    ios_base::sync_with_stdio (false);
    cin . tie (NULL);
    cout . tie (NULL);
    int t , n , k;
    cin >> t;
    while (t --){
        int ans = 1 , o = 0;
        cin >> n >> k;
        cin >> s;
        for(int i = 0 ; i < s . size () ; ++ i)
            ++ vc [(int)s [i]];
        if (k == 1){
            for(int i = 'a' ; i <= 'z' ; ++ i)
                ans = max (ans , vc [(int)i]);
            cout << ans << '\n';
        }
   /*     if (k > n){
            while (n){
                if (k % n == 0){
                    cout << n << '\n';
                    break;
                }
                -- n;
            }
        }
        else*/if (k != 1){
            int f = 1;
            bool p = false ;
            while (f * f <= k){
                if (k % f == 0){
                    if (f > 1)
                        v [++ o] = f;
                    if (f * f != k)
                        v [++ o] = k / f;
                }
                ++ f;
            }
            for(int i = 1 ; i <= o ; ++ i){
                int x = v [i];
                int y = k / x;
                bool pp = true;
                p = true;
                for (int j = 1 ; pp && j * x <= n ; ++ j ){
                    int nr = 0;
                    for(int ij = 'a' ; ij <= 'z' ; ++ ij)
                        nr += vc [ij] / j;
                    if (nr >= x)
                        ans = max (ans , x * j);
                    else
                        pp = false;
                }
            }
            cout << ans << '\n';

        }
        for(int i = 'a' ; i <= 'z' ; ++ i)
            vc [i] = 0;
        for(int i = 1 ; i <= o ; ++ i)
            v [i] = 0;
        s . clear ();
    }
    return 0;
}


#include <iostream>

using namespace std;
int v [2001];
int vc ['z' + 1];
int main()
{
    ios_base::sync_with_stdio (false);
    cin . tie (NULL);
    cout . tie (NULL);
    int t , n , k;
    string s;
    cin >> t;
    while (t --){
        int ans = 1;
        cin >> n >> k;
        cin >> s;
        for(int i = 0 ; i < s . size () ; ++ i)
            ++ vc [(int)s [i]];
        if (k > n){
            while (n){
                if (k % n == 0){
                    cout << n << '\n';
                    break;
                }
                -- n;
            }
        }
        else{
            int f = 1 , ans = 0 , o = 0;
            bool p = false ;
            while (f * f <= k){
                if (k % f == 0){
                    if (f > 1)
                        v [++ o] = f;
                    if (f * f != k)
                        v [++ o] = k / f;
                }
                ++ f;
            }
            for(int i = 1 ; i <= o ; ++ i){
                int x = v [i];
                int y = k / x;
                bool pp = true;
                p = true;
                for (int j = y ; pp && j * x <= n ; ++ j ){
                    int nr = 0;
                    for(int ij = 'a' ; ij <= 'z' ; ++ ij)
                        nr += vc [ij] / j;
                    if (nr >= x)
                        ans = max (ans , x * j);
                    else
                        pp = false;
                }
            }
        cout << ans << '\n';

        }
        for(int i = 'a' ; i <= 'z' ; ++ i)
            vc [i] = 0;
    }
    return 0;
}


#include <iostream>

using namespace std;
int v [2001];
int vc ['z' + 1];
string s;
int main()
{
    ios_base::sync_with_stdio (false);
    cin . tie (NULL);
    cout . tie (NULL);
    int t , n , k;
    cin >> t;
    while (t --){
        int ans = 1 , o = 0;
        cin >> n >> k;
        cin >> s;
        for(int i = 0 ; i < s . size () ; ++ i)
            ++ vc [(int)s [i]];
        if (k == 1){
            for(int i = 'a' ; i <= 'z' ; ++ i)
                ans = max (ans , vc [(int)i]);
            cout << ans << '\n';
        }
        if (k > n){
            while (n){
                if (k % n == 0){
                    cout << n << '\n';
                    break;
                }
                -- n;
            }
        }
        else if (k != 1){
            int f = 1;
            bool p = false ;
            while (f * f <= k){
                if (k % f == 0){
                    if (f > 1)
                        v [++ o] = f;
                    if (f * f != k)
                        v [++ o] = k / f;
                }
                ++ f;
            }
            for(int i = 1 ; i <= o ; ++ i){
                int x = v [i];
                int y = k / x;
                bool pp = true;
                p = true;
                for (int j = y ; pp && j * x <= n ; ++ j ){
                    int nr = 0;
                    for(int ij = 'a' ; ij <= 'z' ; ++ ij)
                        nr += vc [ij] / j;
                    if (nr >= x)
                        ans = max (ans , x * j);
                    else
                        pp = false;
                }
            }
            cout << ans << '\n';

        }
        for(int i = 'a' ; i <= 'z' ; ++ i)
            vc [i] = 0;
        for(int i = 1 ; i <= o ; ++ i)
            v [i] = 0;
        s . clear ();
    }
    return 0;
}