#include using namespace std; using std::cout; using std::cin; using std::endl; using ll=long long; using ld=long double; const ll ILL=2167167167167167167; const int INF=2100000000; const int mod=998244353; #define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++) #define all(p) p.begin(),p.end() template using _pq = priority_queue, greater>; template ll LB(vector &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();} template ll UB(vector &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();} template bool chmin(T &a,T b){if(a>b){a=b;return 1;}else return 0;} template bool chmax(T &a,T b){if(a void So(vector &v) {sort(v.begin(),v.end());} template void Sore(vector &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});} bool yneos(bool a,bool upp=0){if(a){cout<<(upp?"YES\n":"Yes\n");}else{cout<<(upp?"NO\n":"No\n");}return a;} template void vec_out(vector &p,int ty=0){ if(ty==2){cout<<'{';for(int i=0;i<(int)p.size();i++){if(i){cout<<",";}cout<<'"'< T vec_min(vector &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;} template T vec_max(vector &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;} template T vec_sum(vector &a){T ans=T(0);for(auto &x:a) ans+=x;return ans;} int pop_count(long long a){int res=0;while(a){res+=(a&1),a>>=1;}return res;} template bool inside(T l,T x,T r){return l<=x&&x hash, pow; static u64 add(u64 a, u64 b) { a += b; if(a >= mod) a -= mod; return a; } static u64 mul(u64 a, u64 b) { auto c = __uint128_t(a) * b; return add(c >> 61, c & mod); } RollingHash(const string& s): n(s.size()), hash(n + 1), pow(n + 1, 1) { for(ll i = 0; i < n; i++) { pow[i + 1] = mul(pow[i], base); hash[i + 1] = add(mul(hash[i], base), s[i]); } } u64 get(ll l, ll r) const { return add(hash[r], mod - mul(hash[l], pow[r - l])); } }; void solve(); // CYAN / FREDERIC int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t = 1; cin >> t; rep(i, 0, t) solve(); } void solve(){ ll N, M; cin >> N >> M; string S; cin >> S; auto f = [&]() -> vector { RollingHash H(S); vector res(N); using F = pair; F a = {H.get(0, N), H.pow[N]}; auto mul = [&](F l, F r) -> F { l.first = H.add(r.first, H.mul(r.second, l.first)); l.second = H.mul(l.second, r.second); return l; }; auto g = [&](ll t) -> F { F b = a; F c = {0, 1}; while (t){ if (t & 1) c = mul(c, b); t /= 2; b = mul(b, b); } return c; }; rep(i, 0, N){ if (i + M <= N){ res[i] = H.get(i, i + M); continue; } F d = {H.get(i, N), H.pow[N - i]}; d = mul(d, g((i + M) / N - 1)); d = mul(d, {H.get(0, (i + M) % N), H.pow[(i + M) % N]}); res[i] = d.first; } return res; }; int ans = INF; rep(rp, 0, 2){ auto A = f(); reverse(all(S)); auto B = f(); reverse(all(B)); rep(i, 0, N){ if (A[i] == B[(i + M) % N]){ chmin(ans, (int)(i + M - 1) / (int)N); } } M++; } if (ans == INF) cout << "-1\n"; else cout << ans + 1 << "\n"; }