#include using namespace std; #define INF 1234567890 #define ll long long void fast_fourier_transform(vector> &a, const bool invert = false){ // O(n log n) int n = (int)a.size(); for(auto i = 1, j = 0; i < n; ++ i){ int bit = n >> 1; for(; j & bit; bit >>= 1) j ^= bit; j ^= bit; if(i < j) swap(a[i], a[j]); } for(auto len = 1; len < n; len <<= 1){ double theta = acos(-1) / len * (invert ? -1 : 1); complex w(cos(theta), sin(theta)); for(auto i = 0; i < n; i += len << 1){ complex wj(1); for(auto j = 0; j < len; ++ j, wj *= w){ complex u = a[i + j], v = wj * a[i + j + len]; a[i + j] = u + v, a[i + j + len] = u - v; } } } if(invert){ double inv_n = 1.0 / n; for(auto &x: a) x *= inv_n; } } template vector convolute(const vector &p, const vector &q){ if(min(p.size(), q.size()) < 250){ vector res((int)p.size() + (int)q.size() - 1); for(size_t i = 0; i < p.size(); ++ i) for(size_t j = 0; j < q.size(); ++ j) res[i + j] += p[i] * q[j]; return res; } vector> f(p.begin(), p.end()), g(q.begin(), q.end()); int m = max(int(p.size()) + int(q.size()) - 1, 1), n = m; if(__builtin_popcount(n) != 1) n = 1 << __lg(n) + 1; f.resize(n), g.resize(n); fast_fourier_transform(f), fast_fourier_transform(g); for(auto i = 0; i < n; ++ i) f[i] *= g[i]; fast_fourier_transform(f, true); vector res(m); for(auto i = 0; i < m; ++ i) res[i] = round(f[i].real()); return res; } int T; int N, M; string s, t; int S[500501]; int main() { ios::sync_with_stdio(0); cin.tie(0); cin.exceptions(ios::badbit | ios::failbit); cin >> T; while(T--) { cin >> N >> M >> s >> t; reverse(t.begin(), t.end()); for(int i=1; i<=N; i++) S[i] = S[i-1] + (s[i-1] == '?'); vector res(N+M-1); for(char c='a'; c<='z'; c++) { vector A(N), B(M); for(int i=0; i