#include <bits/stdc++.h>
using namespace std;
const int maxn=505;
const int maxm=2000005;
vector<int> sta[maxn];
int bot[maxn],a[maxm];
FILE *in,*out;
inline bool check()
{
    int T,n,m,k,i,t,p,x,y;
    fscanf(in,"%d",&T);
    while(T--)
    {
        fscanf(in,"%d%d%d",&n,&m,&k);
        for(i=0,p=0;i<m;i++) fscanf(in,"%d",&a[i]);
        fscanf(out,"%d",&t);
        if(t<0 || t>2*m) return false;
        for(i=1;i<=n;i++) sta[i].clear(),bot[i]=0;
        while(t--)
            if(fscanf(out,"%d",&i),i==1)
            {
                if(p>=m) return false;
                fscanf(out,"%d",&x);
                if(!(1<=x && x<=n)) return false;
                if((int)sta[x].size()>bot[x] && sta[x].back()==a[p])
                    sta[x].pop_back();
                else sta[x].push_back(a[p]);
                p++;
            }
            else
            {
                fscanf(out,"%d%d",&x,&y);
                if(!(1<=x && x<=n)) return false;
                if(!(1<=y && y<=n)) return false;
                if(x==y) return false;
                if((int)sta[x].size()<=bot[x]) return false;
                if((int)sta[y].size()<=bot[y]) return false;
                if(sta[x][bot[x]]!=sta[y][bot[y]]) return false;
                bot[x]++,bot[y]++;
            }
        if(p!=m) return false;
        for(i=1;i<=n;i++)
            if((int)sta[i].size()!=bot[i])
                return false;
    }
    return true;
}
int main()
{
    int res=0;
    in=fopen("meow.in","r"),out=fopen("meow.out","r");
    if(!check()) printf("failed\n"),res=1;
    fclose(in),fclose(out);
    return res;
}