#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; template vector Manacher(const T& s) { const int n = s.size(); vector res(n); for (int i = 0, r = 1; i < n;) { int l = 2 * i - r; while (0 <= l && r < n && s[l] == s[r]) l--, r++; res[i] = r - i; if (++i != r) { int j = i - 2; while (i + res[j] < r) res[i++] = res[j--]; } } return res; } template vector Manacher_cursor(const T& s) { const int n = s.size(); vector res(n + 1); for (int i = 1, r = 1; i < n;) { int l = 2 * i - r; while (l && r < n && s[l - 1] == s[r]) l--, r++; res[i] = r - i; if (i++ != r) { int j = i - 2; while (i + res[j] < r) res[i++] = res[j--]; } else { r = i; } } return res; } auto floor_div(signed_integral auto x, signed_integral auto y) { return x / y - ((x ^ y) < 0 && x % y != 0); } template T floor_div(T x, unsigned_integral auto y) { return x >= 0 ? T(x / y) : -T(-x / y + (-x % y != 0)); } auto ceil_div(signed_integral auto x, signed_integral auto y) { return x / y + ((x ^ y) >= 0 && x % y != 0); } template T ceil_div(T x, unsigned_integral auto y) { return x >= 0 ? T(x / y + (x % y != 0)) : -T(-x / y); } } int main() { ios::sync_with_stdio(false); cin.tie(0); int tt; cin >> tt; while (tt--) { int n, m; string s; cin >> n >> m >> s; s = s + s + s; constexpr int INF = 1001001001; int ans = INF; { auto mc = Manacher_cursor(s); rep(c, 3 * n + 1) { if (mc[c] >= n || 2 * mc[c] >= m) { int l = c - (m + 1) / 2, r = c + (m + 1) / 2; chmin(ans, floor_div(r - 1, n) - floor_div(l, n) + 1); } } } { auto me = Manacher(s); rep(c, 3 * n) { if (me[c] >= n || 2 * me[c] - 1 >= m) { int l = c - m / 2, r = c + m / 2; chmin(ans, floor_div(r, n) - floor_div(l, n) + 1); } } } if (ans == INF) ans = -1; cout << ans << '\n'; } }