#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef LOCAL # include "debug_print.hpp" # define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else # define debug(...) (static_cast(0)) #endif using namespace std; #define rep(i,n) for(int i=0; i<(n); i++) #define rrep(i,n) for(int i=(n)-1; i>=0; i--) #define FOR(i,a,b) for(int i=(a); i<(b); i++) #define RFOR(i,a,b) for(int i=(b-1); i>=(a); i--) #define ALL(v) v.begin(), v.end() #define RALL(v) v.rbegin(), v.rend() #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ); #define pb push_back using ll = long long; using D = double; using LD = long double; using P = pair; template using PQ = priority_queue>; template using minPQ = priority_queue, greater>; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b ostream &operator<<(ostream &os, const pair &p) { os << p.first << " " << p.second; return os; } template istream &operator>>(istream &is, pair &p) { is >> p.first >> p.second; return is; } template ostream &operator<<(ostream &os, const vector &v) { int s = (int)v.size(); for (int i = 0; i < s; i++) os << (i ? " " : "") << v[i]; return os; } template istream &operator>>(istream &is, vector &v) { for (auto &x : v) is >> x; return is; } void in() {} template void in(T &t, U &...u) { cin >> t; in(u...); } void out() { cout << "\n"; } template void out(const T &t, const U &...u) { cout << t; if (sizeof...(u)) cout << sep; out(u...); } void outr() {} template void outr(const T &t, const U &...u) { cout << t; outr(u...); } #include "atcoder/convolution.hpp" #include "atcoder/string.hpp" /** * @brief Sparse-Table(スパーステーブル) * @docs docs/sparse-table.md */ template< typename T, typename F > struct SparseTable { F f; vector< vector< T > > st; vector< int > lookup; SparseTable() = default; explicit SparseTable(const vector< T > &v, const F &f) : f(f) { const int n = (int) v.size(); const int b = 32 - __builtin_clz(n); st.assign(b, vector< T >(n)); for(int i = 0; i < v.size(); i++) { st[0][i] = v[i]; } for(int i = 1; i < b; i++) { for(int j = 0; j + (1 << i) <= n; j++) { st[i][j] = f(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]); } } lookup.resize(v.size() + 1); for(int i = 2; i < lookup.size(); i++) { lookup[i] = lookup[i >> 1] + 1; } } inline T fold(int l, int r) const { int b = lookup[r - l]; return f(st[b][l], st[b][r - (1 << b)]); } }; template< typename T, typename F > SparseTable< T, F > get_sparse_table(const vector< T > &v, const F &f) { return SparseTable< T, F >(v, f); } void solve(){ int n,m; string s,t; in(n,m,s,t); vector x3_cs(n+1),x_rev(n),x2_rev(n),y(m),y2(m); rep(i,n) { int val = (s[i] == '?' ? 0 : (s[i]-'a')+1); x3_cs[i+1] = x3_cs[i] + val*val*val; x_rev[n-1-i] = val; x2_rev[n-1-i] = val*val; } rep(i,m) { int val = (t[i]-'a')+1; y[i] = val; y2[i] = val*val; } vector pos_match; auto xxy = atcoder::convolution_ll(x2_rev, y); auto xyy = atcoder::convolution_ll(x_rev, y2); rep(i,n-m+1) { ll sum = x3_cs[i+m] - x3_cs[i] - 2 * xxy[n-1-i] + xyy[n-1-i]; if (sum == 0) pos_match.pb(i); } if (pos_match.empty()) { out("-1"); return; } string sr = s; rep(i,n) if(sr[i] == '?') sr[i] = 'a'; auto ztt = atcoder::z_algorithm(t); string u = t+sr; auto sa = atcoder::suffix_array(u); auto lc = atcoder::lcp_array(u, sa); vector sa_inv(n+m); rep(i,n+m) sa_inv[sa[i]] = i; auto spt = get_sparse_table(lc, [](auto a, auto b){return min(a,b);}); int idx = pos_match[0]; FOR(i,1,pos_match.size()) { int pos = pos_match[i]; if (idx+m <= pos) break; int p = sa_inv[0], q = sa_inv[m+pos]; if (p > q) swap(p,q); int len = spt.fold(p,q); chmin(len, n-pos); if (len < n-pos) { char a = t[len], b = sr[idx+len]; if (a > b) { idx = pos; } continue; } len = ztt[pos-idx]; if (len < n-pos+idx) { char a = t[len+pos-idx], b = t[len]; if (a > b) { idx = pos; } continue; } p = sa_inv[idx+m+m], q = sa_inv[n-pos]; if (p > q) swap(p,q); len = spt.fold(p,q); chmin(len, n-pos); if (len < n-pos) { char a = sr[len+idx+m], b = t[n-pos+len]; if (a > b) { idx = pos; } continue; } } string ans = sr; rep(i,m) ans[i+idx] = t[i]; out(ans); } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int ntcs; in(ntcs); while(ntcs--) { solve(); } }