// #define ONLINE_JUDGE
#include "bits/stdc++.h"
using namespace std;
#if !defined(mhnd01s) || defined(ONLINE_JUDGE)
#define print(...) ((void)0)
#endif
using ll = long long;
void solve();
signed main() {
#ifdef mhnd01s
int x = mt19937(random_device()())()%100;printf("%d\n", x);
freopen("out", "wt", stdout);
#else
cin.tie(0)->sync_with_stdio(0);
#endif
cin.exceptions(cin.failbit);
int t = 1;
cin >> t;
while(t--) {
solve();
if(t) cout << '\n';
}return 0;
}
// fast hash
using u64 = uint64_t;
mt19937_64 rng = []{
uint64_t time_entropy = chrono::steady_clock::now().time_since_epoch().count();
uint64_t memory_entropy = (uintptr_t)make_unique<char>().get();
seed_seq ss{time_entropy, memory_entropy};
return mt19937_64(ss);
}();
struct hash61 {
static const uint64_t md = (1LL << 61) - 1;
static uint64_t step;
static vector<uint64_t> pw;
uint64_t addmod(uint64_t a, uint64_t b) const {
a += b;
if (a >= md) a -= md;
return a;
}
uint64_t submod(uint64_t a, uint64_t b) const {
a += md - b;
if (a >= md) a -= md;
return a;
}
uint64_t mulmod(uint64_t a, uint64_t b) const {
uint64_t l1 = (uint32_t) a, h1 = a >> 32, l2 = (uint32_t) b, h2 = b >> 32;
uint64_t l = l1 * l2, m = l1 * h2 + l2 * h1, h = h1 * h2;
uint64_t ret = (l & md) + (l >> 61) + (h << 3) + (m >> 29) + (m << 35 >> 3) + 1;
ret = (ret & md) + (ret >> 61);
ret = (ret & md) + (ret >> 61);
return ret - 1;
}
void ensure_pw(int sz) const {
int cur = (int) pw.size();
if (cur < sz) {
pw.resize(sz);
for (int i = cur; i < sz; i++) {
pw[i] = mulmod(pw[i - 1], step);
}
}
}
vector<uint64_t> pref, suff;
int n;
template<typename T>
hash61(const T& s) {
n = (int) s.size();
ensure_pw(n + 1);
pref.resize(n + 1);
suff.resize(n + 1);
pref[0] = suff[n] = 1;
for (int i = 0; i < n; i++) {
pref[i + 1] = addmod(mulmod(pref[i], step), s[i]);
}
for (int i = n - 1; i >= 0; i--) {
suff[i] = addmod(mulmod(suff[i + 1], step), s[i]);
}
}
uint64_t operator()(const int from, const int to) const {
assert(0 <= from && from <= to && to <= n - 1);
return submod(pref[to + 1], mulmod(pref[from], pw[to - from + 1]));
}
uint64_t rev(const int from, const int to) const {
assert(0 <= from && from <= to && to <= n - 1);
return submod(suff[from], mulmod(suff[to + 1], pw[to - from + 1]));
}
};
uint64_t hash61::step = (md >> 2) + rng() % (md >> 1);
vector<uint64_t> hash61::pw = vector<uint64_t>(1, 1);
void solve() {
int n, m;
string s, t; cin >> n >> m >> s >> t;
if (s.size() < t.size()) return void(cout << -1);
if (s.size() == t.size())
return void(cout << (s == t ? 0 : -1));
// dp[j][i] => 1 + min(dp[k+1][i+k-st+1]) for all matching strings
vector<vector<int>> dp(m+1, vector<int>(n+1, -1));
// hash, vector of starting points in s for that substring hash
map<u64, vector<int>> mp;
hash61 hs(s), ht(t);
for (int sz = 1; sz <= m; sz++)
for (int i = 0; i+sz <= n; i++)
mp[hs(i, i+sz-1)].push_back(i);
function<int(int, int)> rec = [&](int j, int i) -> int {
if (j == m) return i < n; // end of t: check if some characters left in s
if (i == n) return 100000000; // end of s before end of t: invalid
int& ret = dp[j][i];
if (~ret) return ret;
ret = 1e8;
for (int k = j; k < m; k++)
for (int st : mp[ht(j, k)]) { // substring in t matches some starting points in s
if (st < i) continue;
int sz = k-j+1;
int nst = st+sz;
int ost = i+sz;
// if i skipped some characters in s after matching with t
ret = min(ret, bool(nst ^ ost) + rec(k+1, nst));
}
return ret;
};
cout << (rec(0, 0) > n ? -1 : dp[0][0]);
}