#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int n, a[N], b[N], pa[N], pb[N], X, Y;
int t[N * 4][3], pos[N];
int x[3][3] = {
{0, 1, 1},
{1, 1, 2},
{1, 2, 2}
};
int l[N], r[N], s[N];
#define ls (u << 1)
#define rs (u << 1 | 1)
inline void Init(int u, int d)
{
t[u][0] = x[d][0];
t[u][1] = x[d][1];
t[u][2] = x[d][2];
}
inline void PushUp(int u)
{
t[u][0] = t[rs][t[ls][0]];
t[u][1] = t[rs][t[ls][1]];
t[u][2] = t[rs][t[ls][2]];
}
inline void Build(int u, int a, int b)
{
if(a == b)
{
pos[a] = u;
Init(u, 1);
return ;
}
int mid = (a + b) >> 1;
Build(ls, a, mid);
Build(rs, mid + 1, b);
PushUp(u);
}
inline void Modify(int i)
{
int u = pos[i];
Init(u, (a[i] <= X) + (b[i] > Y));
for(u >>= 1; u; u >>= 1) PushUp(u);
}
inline bool Query()
{
return t[1][(a[1] <= X) + (b[1] > Y)] == 2;
}
inline void Work(int res[])
{
for(int i = 1; i <= n; i ++ )
pa[a[i]] = pb[b[i]] = i;
Build(1, 2, n);
X = 0, Y = 0;
for(int i = 1; i <= n; i ++ )
{
X = i;
if(pa[i] > 1) Modify(pa[i]);
while(Y < n && Query())
{
Y ++ ;
if(pb[Y] > 1) Modify(pb[Y]);
}
res[i] = Y;
}
}
int main()
{
int T;
scanf("%d", &T);
while(T -- )
{
scanf("%d", &n);
for(int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);
for(int i = 1; i <= n; i ++ ) scanf("%d", &b[i]);
Work(r);
for(int i = 1; i <= n; i ++ )
{
a[i] = n - a[i] + 1;
b[i] = n - b[i] + 1;
}
Work(l);
reverse(l + 1, l + n + 1);
for(int i = 1; i <= n; i ++ )
l[i] = n - l[i] + 1;
for(int i = 1; i <= n; i ++ )
{
// printf("#%d %d %d\n", i, l[i], r[i]);
if(l[i] > r[i]) continue ;
if(i >= r[i])
{
s[i - r[i]] ++ ;
s[i - l[i] + 1] -- ;
}
else if(i <= l[i])
{
s[l[i] - i] ++ ;
s[r[i] - i + 1] -- ;
}
else
{
s[0] ++;
s[1] ++ ;
s[i - l[i] + 1] -- ;
s[r[i] - i + 1] -- ;
}
}
for(int i = 1; i < n; i ++ ) s[i] += s[i - 1];
for(int i = 0; i < n; i ++ ) printf("%d ", s[i]);
puts("");
for(int i = 0; i <= n; i ++ ) l[i] = r[i] = s[i] = 0;
}
return 0;
}