#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 #define REP_OVERLOAD(arg1, arg2, arg3, arg4, NAME, ...) NAME #define REP3(i, l, r, s) \ for (int i = int(l), rep3_r = int(r), rep3_s = int(s); i < rep3_r; \ i += rep3_s) #define REP2(i, l, r) REP3(i, l, r, 1) #define REP1(i, n) REP2(i, 0, n) #define rep(...) REP_OVERLOAD(__VA_ARGS__, REP3, REP2, REP1, )(__VA_ARGS__) #define repin(i, l, r) for (int i = int(l), repin_r = int(r); i <= repin_r; ++i) #define RREP_OVERLOAD(arg1, arg2, arg3, arg4, NAME, ...) NAME #define RREP3(i, l, r, s) \ for (int i = int(r) - 1, rrep3_l = int(l), rrep3_s = int(s); i >= rrep3_l; \ i -= rrep3_s) #define RREP2(i, l, r) RREP3(i, l, r, 1) #define RREP1(i, n) RREP2(i, 0, n) #define rrep(...) RREP_OVERLOAD(__VA_ARGS__, RREP3, RREP2, RREP1, )(__VA_ARGS__) #define rrepin(i, l, r) \ for (int i = int(r), rrepin_l = int(l); i >= rrepin_l; --i) #include #include #include namespace rklib { // a <- max(a, b) template bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } // a <- min(a, b) template bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } // if a < 0: a <- b // else: a <- min(a, b) template bool chmin_non_negative(T &a, const T &b) { if (a < 0 || a > b) { a = b; return true; } return false; } // floor(num / den) template T div_floor(T num, T den) { if (den < 0) num = -num, den = -den; return num >= 0 ? num / den : (num + 1) / den - 1; } // ceil(num / den) template T div_ceil(T num, T den) { if (den < 0) num = -num, den = -den; return num <= 0 ? num / den : (num - 1) / den + 1; } namespace internal { template T remainder_count(T r, T b, T m) { return r / m * b + std::min(b, r % m); } } // namespace internal // Number of integer x s.t. // - x in [l, r) // - x mod m in [a, b) template T remainder_count(T l, T r, T a, T b, T m) { assert(l >= 0); assert(m >= 1); if (l >= r || a >= b) return 0; if (m <= a || b < 0) return 0; chmax(a, T(0)); chmin(b, m); auto res = internal::remainder_count(r, b, m); if (l >= 1) res -= internal::remainder_count(l, b, m); if (a >= 1) res -= internal::remainder_count(r, a, m); if (l >= 1 && a >= 1) res += internal::remainder_count(l, a, m); return res; } } // namespace rklib using namespace std; using namespace rklib; using lint = long long; using pii = pair; using pll = pair; #include #include #include namespace rklib { struct RollingHash { private: using ulint = unsigned long long; const ulint hash_mod = (1ULL << 61ULL) - 1, hash_base = 1'000'000'007; const ulint mask_30bit = (1ULL << 30ULL) - 1, mask_31bit = (mask_30bit << 1ULL) + 1; const ulint num = hash_mod << 2LL; std::vector pow_table, hash; int n; ulint calc_mul31(ulint a, ulint b) { ulint au = a >> 31ULL, ad = a & mask_31bit; ulint mid = au * b, midu = mid >> 30ULL, midd = mid & mask_30bit; return midu + (midd << 31ULL) + ad * b; } ulint calc_mul61(ulint a, ulint b) { ulint au = a >> 31ULL, ad = a & mask_31bit; ulint bu = b >> 31ULL, bd = b & mask_31bit; ulint mid = au * bd + ad * bu, midu = mid >> 30ULL, midd = mid & mask_30bit; return 2 * au * bu + midu + (midd << 31ULL) + ad * bd; } ulint calc_mod(ulint x) { x = (x & hash_mod) + (x >> 61ULL); if (x > hash_mod) x -= hash_mod; return x; } public: RollingHash(std::string &s) { n = s.size(); pow_table.resize(n + 1); hash.resize(n + 1); pow_table[0] = 1ULL; for (int i = 0; i < n; ++i) pow_table[i + 1] = calc_mod(calc_mul61(pow_table[i], hash_base)); hash[0] = 0ULL; for (int i = 0; i < n; ++i) hash[i + 1] = calc_mod(calc_mul61(hash[i], hash_base) + s[i]); } ulint slice(int l, int r) { return calc_mod(hash[r] + num - calc_mul61(hash[l], pow_table[r - l])); } }; } // namespace rklib vector wildcard_matching(string s, string t) { int n = s.size(), m = t.size(); vector> pow_s(4, vector(n, 1)); vector> pow_t(4, vector(m, 1)); rep(k, 1, 4) rep(i, n) { pow_s[k][i] = pow_s[k - 1][i] * (s[i] == '?' ? 0 : s[i] - 'a'); } reverse(t.begin(), t.end()); rep(k, 1, 4) rep(i, m) { pow_t[k][i] = pow_t[k - 1][i] * (t[i] == '?' ? 0 : t[i] - 'a'); } auto v1 = atcoder::convolution_ll(pow_s[3], pow_t[1]); auto v2 = atcoder::convolution_ll(pow_s[2], pow_t[2]); auto v3 = atcoder::convolution_ll(pow_s[1], pow_t[3]); vector res; repin(i, m - 1, n - 1) { auto tmp = v1[i] - v2[i] * 2 + v3[i]; if (tmp == 0) res.push_back(i - (m - 1)); } return res; } void solve() { int n, m; string s, t; cin >> n >> m >> s >> t; auto pos = wildcard_matching(s, t); if (pos.empty()) { cout << "-1\n"; return; } rep(i, n) if (s[i] == '?') s[i] = 'a'; auto lcp = [](RollingHash &rs, RollingHash &rt, int l1, int l2, int len) { if (rs.slice(l1, l1 + len) == rt.slice(l2, l2 + len)) return len; int low = 0, high = len; while (high - low > 1) { int mid = (high + low) / 2; (rs.slice(l1, l1 + mid) == rt.slice(l2, l2 + mid) ? low : high) = mid; } return low; }; RollingHash rs(s), rt(t); auto p = *min_element(pos.begin(), pos.end(), [&](const int &i, const int &j) { int a = i, b = j; if (a > b) swap(a, b); if (b <= a + m) { int len = lcp(rt, rs, 0, a, b - a); if (len < b - a) { return t[len] < s[a + len] ? a : b; } len = lcp(rt, rt, b - a, 0, a + m - b); if (len < a + m - b) { return t[b - a + len] < t[len] ? a : b; } len = lcp(rs, rt, 0, a + m - b, b - a); if (len < b - a) { return s[len] < t[a + m - b + len] ? a : b; } return a; } else { int len = lcp(rt, rs, 0, a, m); if (len < m) { return t[len] < s[a + len] ? a : b; } len = lcp(rs, rs, 0, a + m, b - a - m); if (len < b - a - m) { return s[len] < s[a + m + len] ? a : b; } len = lcp(rs, rt, b - a - m, 0, m); if (len < m) { return s[len + b - a - m] < t[len] ? a : b; } return a; } }); rep(i, p, p + m) s[i] = t[i - p]; cout << s << "\n"; } int main() { int t; cin >> t; rep(tt, t) solve(); }