#include <bits/stdc++.h>
using namespace std;
// Modulus as given:
static const int MOD = 998244353;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while(t--){
int n;
cin >> n;
vector<int> p(n), q(n);
for(int i = 0; i < n; i++){
cin >> p[i];
}
for(int i = 0; i < n; i++){
cin >> q[i];
}
// 1) Build inverse‐permutations posP, posQ in O(n)
vector<int> posP(n), posQ(n);
for(int i = 0; i < n; i++){
posP[ p[i] ] = i;
posQ[ q[i] ] = i;
}
// 2) Build prefix‐max arrays P[i] = max(p[0..i]), Q[i] = max(q[0..i]) in O(n)
vector<int> P(n), Q(n);
int curP = -1, curQ = -1;
for(int i = 0; i < n; i++){
curP = max(curP, p[i]);
P[i] = curP;
curQ = max(curQ, q[i]);
Q[i] = curQ;
}
// 3) Precompute pow2[x] = 2^x mod MOD for x=0..n-1 in O(n)
vector<int> pow2(n);
pow2[0] = 1;
for(int x = 1; x < n; x++){
pow2[x] = int((2LL * pow2[x-1]) % MOD);
}
// 4) Now compute each r[i] in O(1) using the 3‐case logic:
// r[i] = 2^{M_i} + 2^{m_i} (mod MOD).
vector<int> ans(n);
for(int i = 0; i < n; i++){
int MP = P[i];
int MQ = Q[i];
int M = max(MP, MQ);
int m_i;
if(MP > MQ){
// Case A: M = MP. The only j that attains max=MP is j0 = posP[MP].
int j0 = posP[MP]; // guaranteed j0 <= i
m_i = q[i - j0];
}
else if(MQ > MP){
// Case B: M = MQ. Only j that attains max=MQ is j0 = i - posQ[MQ].
int k0 = posQ[MQ]; // k0 <= i
int j0 = i - k0; // that lies in [0..i]
m_i = p[j0];
}
else {
// Case C: MP == MQ == M.
int jP = posP[M]; // <= i
int jQ = posQ[M]; // <= i
int s1 = q[i - jP]; // “smaller exponent” if p_j=M
int s2 = p[i - jQ]; // “smaller exponent” if q_{i-j}=M
m_i = max(s1, s2);
}
// Finally
long long big = pow2[M];
long long small = pow2[m_i];
ans[i] = int( (big + small) % MOD );
}
// Print r[0..n-1] on one line:
for(int i = 0; i < n; i++){
cout << ans[i] << (i+1 < n ? ' ' : '\n');
}
}
return 0;
}