#include using namespace std; #define all(v) (v).begin(),(v).end() #define pb(a) push_back(a) #define rep(i, n) for(int i=0;i hash1, hash2, power1, power2; std::vector hash1_, hash2_, power1_, power2_; void rev_init(std::string S) { std::reverse(S.begin(), S.end()); int n = (int)S.size(); hash1_.assign(n+1, 0); hash2_.assign(n+1, 0); power1_.assign(n+1, 1); power2_.assign(n+1, 1); for (int i = 0; i < n; ++i) { hash1_[i+1] = (hash1_[i] * base1 + S[i]) % mod1; hash2_[i+1] = (hash2_[i] * base2 + S[i]) % mod2; power1_[i+1] = (power1_[i] * base1) % mod1; power2_[i+1] = (power2_[i] * base2) % mod2; } } // construct RollingHash(const std::string &S, int rev = 0) { int n = (int)S.size(); hash1.assign(n+1, 0); hash2.assign(n+1, 0); power1.assign(n+1, 1); power2.assign(n+1, 1); for (int i = 0; i < n; ++i) { hash1[i+1] = (hash1[i] * base1 + S[i]) % mod1; hash2[i+1] = (hash2[i] * base2 + S[i]) % mod2; power1[i+1] = (power1[i] * base1) % mod1; power2[i+1] = (power2[i] * base2) % mod2; } if(rev) rev_init(S); } // get hash of S[left:right) inline std::pair get(int l, int r) const { long long res1 = hash1[r] - hash1[l] * power1[r-l] % mod1; if (res1 < 0) res1 += mod1; long long res2 = hash2[r] - hash2[l] * power2[r-l] % mod2; if (res2 < 0) res2 += mod2; return {res1, res2}; } inline std::pair get_rev(int l, int r) const { long long res1 = hash1_[r] - hash1_[l] * power1_[r-l] % mod1; if (res1 < 0) res1 += mod1; long long res2 = hash2_[r] - hash2_[l] * power2_[r-l] % mod2; if (res2 < 0) res2 += mod2; return {res1, res2}; } // [l, r) is palindrome inline bool is_palindrome(int l, int r, int n) const { int l_rev = n - r; int r_rev = n - l; return get(l, r) == get_rev(l_rev, r_rev); } // get lcp of S[a:) and T[b:) inline int getLCP(int a, int b) const { int len = std::min((int)hash1.size()-a, (int)hash1.size()-b); int low = -1, high = len; while (high - low > 1) { int mid = (low + high) / 2; if (get(a, a+mid) != get(b, b+mid)) high = mid; else low = mid; } return low; } }; void solve() { ll n, m; string s; cin >> n >> m >> s; if(n >= m) { RollingHash r(s, 1); bool ok = 0; rep(i, n - m + 1) { if(r.get(i, i + m) == r.get_rev(i, i + m)) { ok = true; break; } } if(ok) { cout << 1 << endl; return; } } ll ans = INF; if(n * 2 >= m) { RollingHash r(s + s, 1); bool ok = 0; rep(i, n * 2 - m + 1) { if(r.get(i, i + m) == r.get_rev(i, i + m)) { ok = true; break; } } if(ok) { cout << 1 << endl; return; } } s = s + s + s; int sz = s.size(); RollingHash r(s, 1); { ll k = (m + 1) / 2; k *= 2; for(int i = n; i < (n << 1); i ++) { ll num = min(i, sz - i); if(r.is_palindrome(i - num, i + num, n * 3)) { ll val = 1; ll ri = sz - i - n; if(k / 2 > ri) { ll r = k / 2 - ri; val += (r + n - 1) / n; } ll le = i - n; if(k / 2 > le) { ll l = k / 2 - le; val += (l + n - 1) / n; } ans = min(ans, val); } } } { ll k = (m + 1) / 2; k *= 2; k ++; for(int i = n; i < (n << 1); i ++) { ll num = min(i, sz - i - 1); // cout << i << " " << num << endl; // if(r.get(i - num, i + num + 1) == r.get_rev(i - num, i + num + 1)) if(r.is_palindrome(i - num, i + num + 1, n * 3)) { ll val = 1; ll ri = sz - i - 1 - n; if(k / 2 > ri) { ll r = k / 2 - ri; val += (r + n - 1) / n; } ll le = i - n; if(k / 2 > le) { ll l = k / 2 - le; val += (l + n - 1) / n; } // cout << i << " " << val << endl; ans = min(ans, val); } } } cout << (ans == INF ? -1 : ans) << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); int t; cin >> t; rep(i, t) solve(); return 0; }