#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef unsigned long long ll; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int maxn = 1e5+1000; int n; int sum[maxn]; int b[maxn], c[maxn], z; const ll mod = 998244353; int lowbit(int t) { return t&(-t); } void update(int pos,int c) { while(pos<=n){ sum[pos] += c; pos += lowbit(pos); } } int getsum(int pos) { int s = 0; while(pos>0){ s += sum[pos]; pos -= lowbit(pos); } return s; } /* void push_down(int rt) { sum[rt] = sum[rt<<1]+sum[rt<<1|1]; } void build(int l,int r,int rt) { if(l==r){ scanf("%d",&b[z]); sum[rt]+=num[b[z]]; z++; return ; } int m = (l+r)>>1; build(lson); build(rson); push_down(rt); } int getsum(int L,int R,int l,int r,int rt,int flag) { if(L<=l&&R>=r){ if(flag) { flag = 0; sum[rt] -= 1; } return sum[rt]+1; } int m = (l+r)>>1; int res = 0; if(L<=m){ res += getsum(L,R,lson); } if(R>m) res += getsum(L,R,rson); push_down(rt); return res; } void update(int L,int R,int l,int r,int rt) { }*/ int main() { int T; cin>>T; while(T--) { scanf("%d",&n); memset(sum, 0, sizeof sum); z = 0; for(int i = 0; i < n; i++){ scanf("%d",&b[i]); update(i+1,1); } // build(1,n,1); for(int i = 0; i < n; i++) scanf("%d",&c[i]); if(b[0]!=c[0]){ printf("0\n"); continue; } int mis, mas, flag; ll ans; mis = mas = b[0]; int temp = getsum(b[0])-getsum(b[0]-1); ans = temp; update(b[0],-1); // cout<<"temp = "<