#include <stdio.h>
#include <vector>
using namespace std;

int main() {
    int t;
    scanf("%d", &t);

    while(t--) {
        int n, p[200010]={}, q[200010]={};
        vector<int> v;
        scanf("%d", &n);

        for(int i=0; i<n; i++) {
            scanf("%d", &p[i]);
        }
        for(int i=0; i<n; i++) {
            scanf("%d", &q[i]);

            if(i==0) v.push_back(q[i]);
            if(i>0&&q[i]!=q[i-1]) v.push_back(q[i]);
        }

        int i, ptr=0;
        for(i=0; i<v.size();) {
            printf("%d ", v[i]);
            if(ptr==n) break;
            if(p[ptr]!=v[i]) i++;
            else ptr++;
        }

        if(ptr==n||i==v.size()) printf("YES\n");
        else printf("NO\n");

    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: