#include <bits/stdc++.h>
#include <atcoder/all>
//using namespace std;
using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
using mint = modint998244353;
const int mod = 998244353;
//const int INF = 1e9;
const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r) - 1;i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }

// startのindexから右にsize分のHashを計算する
// 更新がある場合はSegment Treeを使う
mint calcHash(int n, int start, long long size, const std::vector<mint> &hashArray, const int base) {
	long long h = std::min(size, (long long)n - start);
	long long b = ((size - h) / n) * n;
	long long t = size - h - b;
	mint hash = 0;
	if (h) {
		mint sub = (hashArray[start + h] - hashArray[start]) / atcoder::pow_mod(base, start, mod);
		hash += sub;
		//cout << sub.val() << endl;
	}
	if (b) {
		mint sub = hashArray[n] * (atcoder::pow_mod(base, b, mod) - 1) / (atcoder::pow_mod(base, n, mod) - 1) * atcoder::pow_mod(base, h, mod);
		hash += sub;
	}
	if (t) {
		mint sub = atcoder::pow_mod(base, h + b, mod)*hashArray[t];
		hash += sub;
	}
	return hash;
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	int t; std::cin >> t;
	const int base = 100;
	std::vector<mint>pow(200005);
	pow[0] = 1;
	rep2(i, 1, pow.size())pow[i] = pow[i - 1] * base;
	while (t--) {
		int n, m; std::cin >> n >> m;
		std::string s; std::cin >> s;
		auto t = s;
		reverse(all(t));
		long long ans = LINF;
		std::vector<mint>hs(s.size() + 1), ht(t.size() + 1);
		rep(i, n) {
			hs[i + 1] = hs[i] + pow[i] * (s[i] - 'a' + 1);
			ht[i + 1] = ht[i] + pow[i] * (t[i] - 'a' + 1);
		}
		rep(i, n) {
			int sz = (m) / 2;
			int iR = (i + 1) % n;
			mint hR = calcHash(n, iR, sz, hs, base);
			int iL = (i - 1 + n) % n;
			iL = n - 1 - iL;
			mint hL = calcHash(n, iL, sz, ht, base);
			if (hR != hL)continue;
			int tmp = 1;
			int l = sz - i;
			if (l > 0)tmp += (l + n - 1) / n;
			int r = sz - (n - 1 - i);//???
			if (r > 0)tmp += (r + n - 1) / n;
			chmin(ans, tmp);
		}
		rep(i, n) {
			int sz = (m + 1) / 2;
			int iR = (i + 0) % n;
			mint hR = calcHash(n, iR, sz, hs, base);
			int iL = (i - 1 + n) % n;
			iL = n - 1 - iL;
			mint hL = calcHash(n, iL, sz, ht, base);
			if (hR != hL)continue;
			int tmp = 0 != i;
			int l = sz - i;
			int r = 0 == i ? sz : sz - (n - i);
			//cout << l << " " << r << endl;
			if (l > 0)tmp += (l + n - 1) / n;
			if (r > 0)tmp += (r + n - 1) / n;
			//cout << i << endl;
			chmin(ans, tmp);
		}
		if (LINF == ans)ans = -1;
		std::cout << ans << std::endl;
	}
	return 0;
}