#include using namespace std; const int maxn=5e5+5; int sp[maxn][20]; template vector suffix_array(int n, const T &s, int char_bound) { vector a(n); if (n == 0) { return a; } if (char_bound != -1) { vector aux(char_bound, 0); for (int i = 0; i < n; i++) { aux[s[i]]++; } int sum = 0; for (int i = 0; i < char_bound; i++) { int add = aux[i]; aux[i] = sum; sum += add; } for (int i = 0; i < n; i++) { a[aux[s[i]]++] = i; } } else { iota(a.begin(), a.end(), 0); sort(a.begin(), a.end(), [&s](int i, int j) { return s[i] < s[j]; }); } vector sorted_by_second(n); vector ptr_group(n); vector new_group(n); vector group(n); group[a[0]] = 0; for (int i = 1; i < n; i++) { group[a[i]] = group[a[i - 1]] + (!(s[a[i]] == s[a[i - 1]])); } int cnt = group[a[n - 1]] + 1; int step = 1; while (cnt < n) { int at = 0; for (int i = n - step; i < n; i++) { sorted_by_second[at++] = i; } for (int i = 0; i < n; i++) { if (a[i] - step >= 0) { sorted_by_second[at++] = a[i] - step; } } for (int i = n - 1; i >= 0; i--) { ptr_group[group[a[i]]] = i; } for (int i = 0; i < n; i++) { int x = sorted_by_second[i]; a[ptr_group[group[x]]++] = x; } new_group[a[0]] = 0; for (int i = 1; i < n; i++) { if (group[a[i]] != group[a[i - 1]]) { new_group[a[i]] = new_group[a[i - 1]] + 1; } else { int pre = (a[i - 1] + step >= n ? -1 : group[a[i - 1] + step]); int cur = (a[i] + step >= n ? -1 : group[a[i] + step]); new_group[a[i]] = new_group[a[i - 1]] + (pre != cur); } } swap(group, new_group); cnt = group[a[n - 1]] + 1; step <<= 1; } return a; } template vector suffix_array(const T &s, int char_bound) { return suffix_array((int) s.size(), s, char_bound); } template vector build_lcp(int n, const T &s, const vector &sa) { assert((int) sa.size() == n); vector pos(n); for (int i = 0; i < n; i++) { pos[sa[i]] = i; } vector lcp(max(n - 1, 0)); int k = 0; for (int i = 0; i < n; i++) { k = max(k - 1, 0); if (pos[i] == n - 1) { k = 0; } else { int j = sa[pos[i] + 1]; while (i + k < n && j + k < n && s[i + k] == s[j + k]) { k++; } lcp[pos[i]] = k; } } return lcp; } template vector build_lcp(const T &s, const vector &sa) { return build_lcp((int) s.size(), s, sa); } typedef long long ll; const int p=998244353; int po(int a,int b) {if(b==0) return 1; if(b==1) return a; if(b%2==0) {int u=po(a,b/2);return (u*1LL*u)%p;} else {int u=po(a,b-1);return (a*1LL*u)%p;}} int inv(int x) {return po(x,p-2);} template struct Fft { // 1, 1/4, 1/8, 3/8, 1/16, 5/16, 3/16, 7/16, ... int g[1 << (K - 1)]; constexpr Fft() : g() { //if tl constexpr... static_assert(K >= 2, "Fft: K >= 2 must hold"); g[0] = 1; g[1 << (K - 2)] = G; for (int l = 1 << (K - 2); l >= 2; l >>= 1) { g[l >> 1] = (static_cast(g[l]) * g[l]) % M; } assert((static_cast(g[1]) * g[1]) % M == M - 1); for (int l = 2; l <= 1 << (K - 2); l <<= 1) { for (int i = 1; i < l; ++i) { g[l + i] = (static_cast(g[l]) * g[i]) % M; } } } void fft(vector &x) const { const int n = x.size(); assert(!(n & (n - 1)) && n <= 1 << K); for (int h = __builtin_ctz(n); h--; ) { const int l = 1 << h; for (int i = 0; i < n >> 1 >> h; ++i) { for (int j = i << 1 << h; j < ((i << 1) + 1) << h; ++j) { const int t = (static_cast(g[i]) * x[j | l]) % M; if ((x[j | l] = x[j] - t) < 0) x[j | l] += M; if ((x[j] += t) >= M) x[j] -= M; } } } for (int i = 0, j = 0; i < n; ++i) { if (i < j) std::swap(x[i], x[j]); for (int l = n; (l >>= 1) && !((j ^= l) & l); ) {} } } vector convolution(const vector &a, const vector &b) const { if(a.empty() || b.empty()) return {}; const int na = a.size(), nb = b.size(); int n, invN = 1; for (n = 1; n < na + nb - 1; n <<= 1) invN = ((invN & 1) ? (invN + M) : invN) >> 1; vector x(n, 0), y(n, 0); std::copy(a.begin(), a.end(), x.begin()); std::copy(b.begin(), b.end(), y.begin()); fft(x); fft(y); for (int i = 0; i < n; ++i) x[i] = (((static_cast(x[i]) * y[i]) % M) * invN) % M; std::reverse(x.begin() + 1, x.end()); fft(x); x.resize(na + nb - 1); return x; } }; Fft<998244353,23,31> muls; vector operator *(vector v1,vector v2) { return muls.convolution(v1,v2); } int32_t main() { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); /*string s="abbaaaf"; vector sa=suffix_array(s,256); vector lc=build_lcp(s,sa); for(int i:sa) cout<>t; while(t--) { int n,m;cin>>n>>m; string s1,s2;cin>>s1>>s2; string s0=s1; bool ok[n-m+1];for(int i=0;i<=n-m;++i) ok[i]=true; for(char c='a';c<='z';++c) { vector v1(n),v2(m); int cnt=0; for(int i=0;i h=v1*v2; for(int i=m-1;i<=n-1;++i) { ok[n-i-1]&=(h[i]==cnt); } } for(int i=0;i sa=suffix_array(s,256); vector lc=build_lcp(s,sa); vector pos(s.size()); for(int i=0;ij) swap(i,j); if(i==j) return inf; int o=31-__builtin_clz(j-i); return min(sp[i][o],sp[j-(1<bool { if(i==j) return false; bool sw=false; if(i>j) {swap(i,j);sw=true;} { if(j-i v; for(int i=0;i<=n-m;++i) if(ok[i]) v.push_back(i); int pos1=(*min_element(v.begin(),v.end(),cmp)); string ans=s1.substr(0,pos1); ans+=s2; ans+=s1.substr(pos1+m,n-m-pos1); /*if(ans!=stupid) { cout<