#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 #include #include #include using namespace std; using lint = long long; using pint = pair; using plint = pair; struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template void ndarray(vector& vec, const V& val, int len) { vec.assign(len, val); } template void ndarray(vector& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); } template bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; } template bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; } const std::vector> grid_dxs{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); } template T1 floor_div(T1 num, T2 den) { return (num > 0 ? num / den : -((-num + den - 1) / den)); } template std::pair operator+(const std::pair &l, const std::pair &r) { return std::make_pair(l.first + r.first, l.second + r.second); } template std::pair operator-(const std::pair &l, const std::pair &r) { return std::make_pair(l.first - r.first, l.second - r.second); } template std::vector sort_unique(std::vector vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template int arglb(const std::vector &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); } template int argub(const std::vector &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); } template IStream &operator>>(IStream &is, std::vector &vec) { for (auto &v : vec) is >> v; return is; } template OStream &operator<<(OStream &os, const std::vector &vec); template OStream &operator<<(OStream &os, const std::array &arr); template OStream &operator<<(OStream &os, const std::unordered_set &vec); template OStream &operator<<(OStream &os, const pair &pa); template OStream &operator<<(OStream &os, const std::deque &vec); template OStream &operator<<(OStream &os, const std::set &vec); template OStream &operator<<(OStream &os, const std::multiset &vec); template OStream &operator<<(OStream &os, const std::unordered_multiset &vec); template OStream &operator<<(OStream &os, const std::pair &pa); template OStream &operator<<(OStream &os, const std::map &mp); template OStream &operator<<(OStream &os, const std::unordered_map &mp); template OStream &operator<<(OStream &os, const std::tuple &tpl); template OStream &operator<<(OStream &os, const std::vector &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } template OStream &operator<<(OStream &os, const std::array &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; } template std::istream &operator>>(std::istream &is, std::tuple &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; } template OStream &operator<<(OStream &os, const std::tuple &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; } template OStream &operator<<(OStream &os, const std::unordered_set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::deque &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template OStream &operator<<(OStream &os, const std::set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::unordered_multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::pair &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; } template OStream &operator<<(OStream &os, const std::map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::unordered_map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } #ifdef HITONANODE_LOCAL const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m"; #define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl #define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl : std::cerr) #else #define dbg(x) ((void)0) #define dbgif(cond, x) ((void)0) #endif uint32_t rand_int() // XorShift random integer generator { static uint32_t x = 123456789, y = 362436069, z = 521288629, w = 88675123; uint32_t t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } double rand_double() { return (double)rand_int() / UINT32_MAX; } #include #include #include #include int op(int l, int r) { return std::min(l, r); } int e() { return 0; } string solve(const string &S1_, const string &S2) { const int N = S1_.size(), M = S2.size(); string S1a = S1_; for (auto &c : S1a) { if (c == '?') c = 'a'; } const auto z_algo = atcoder::z_algorithm(S2 + "_" + S1a); const auto sa = atcoder::suffix_array(S2 + "_" + S1a); vector sainv(sa.size()); REP(i, sa.size()) sainv.at(sa.at(i)) = i; const auto lcp = atcoder::lcp_array(S2 + "_" + S1a, sa); atcoder::segtree lcptree(lcp); REP(_, 1000) rand_int(); vector hash(26); for (auto &x : hash) x = rand_int(); decltype(hash) f, g; for (auto c : S1_) { if (c == '?') { f.push_back(0); } else { f.push_back(hash.at(c - 'a')); } } auto f2 = f; for (auto &x : f2) x *= x; auto f3 = f2; REP(i, f3.size()) f3.at(i) *= f.at(i); for (auto c : S2) g.push_back(hash.at(c - 'a')); reverse(ALL(g)); auto g2 = g; for (auto &x : g2) x *= x; decltype(g) g0(g.size(), 1); auto conv = atcoder::convolution(g0, f3), c2 = atcoder::convolution(g, f2), c3 = atcoder::convolution(g2, f); REP(i, conv.size()) conv.at(i) += c3.at(i) - c2.at(i) * 2; vector heads; FOR(i, M - 1, N) if (conv.at(i) == 0) heads.push_back(i - (M - 1)); if (heads.empty()) return "-1"; // -1: same auto solve = [&](int l, int r) -> int { assert(l < r); if (r - l < M) { if (int z = z_algo.at(M + 1 + l); z < r - l) { auto lhs = S2.at(z), rhs = S1a.at(l + z); assert(lhs != rhs); return lhs < rhs; } if (int z = z_algo.at(r - l); z < M - (r - l)) { auto lhs = S2.at(r - l + z), rhs = S2.at(z); return lhs < rhs; } int lcur = sainv.at(M + 1 + l + M), rcur = sainv.at(M - (r - l)); int matchlen = lcptree.prod(min(lcur, rcur), max(lcur, rcur)); if (matchlen >= r - l) return -1; auto lhs = S1a.at(l + M + matchlen), rhs = S2.at(M - (r - l) + matchlen); return lhs < rhs; } else { int lcur = sainv.at(0), rcur = sainv.at(M + 1 + l); int match = lcptree.prod(min(lcur, rcur), max(lcur, rcur)); if (match < M) { auto lhs = S2.at(match); auto rhs = S1a.at(l + match); assert(lhs != rhs); return lhs < rhs; } lcur = sainv.at(M + 1 + r), rcur = sainv.at(0); match = lcptree.prod(min(lcur, rcur), max(lcur, rcur)); if (match < M) { auto lhs = S1a.at(r + match); auto rhs = S2.at(match); return lhs < rhs; } return -1; } }; auto comp = [&](int l, int r) -> bool { if (l == r) return false; if (l < r) { auto ret = solve(l, r); if (ret < 0) return false; return ret; } else { auto ret = solve(r, l); if (ret < 0) return false; return ret ^ 1; } }; const int h = *std::min_element(heads.begin(), heads.end(), comp); string ret = S1a.substr(0, h) + S2 + S1a.substr(h + M); return ret; } int main() { int T; cin >> T; while (T--) { int N, M; string S1, S2; cin >> N >> M >> S1 >> S2; cout << solve(S1, S2) << '\n'; } }