#include <bits/stdc++.h>
using namespace std;
const int N = 1005,M = 2000005;
int n,m,kk,a[M];deque<int> stk[N];
int main ()
{
    ios::sync_with_stdio(false);
    ifstream inp("meow.in"),oup("meow.out");
    int T;inp >> T;
    while (T--)
    {
        // cerr << "New Testcase" << endl;
        inp >> n >> m >> kk;
        for (int i = 1;i <= m;i++)
        {
            inp >> a[i];if (a[i] < 1 || a[i] > kk) {cerr << "Error" << i << ' ' << a[i] << endl;return 1;}
            assert(1 <= a[i] && a[i] <= kk);
        }
        int op,idx = 1;oup >> op;
        if (op < m || op > m * 2) return 5;
        while (op--)
        {
            int tt;oup >> tt;
            if (tt == 1)
            {
                if (idx > m) return 4;
                int a,b;oup >> a;b = ::a[idx++];stk[a].push_back(b);
                assert(1 <= a && a <= n);
                if (stk[a].size() >= 2 && stk[a].back() == stk[a][stk[a].size() - 2])
                    stk[a].pop_back(),stk[a].pop_back();
            }
            else
            {
                int a,b;oup >> a >> b;if (stk[a][0] == stk[b][0]) stk[a].pop_front(),stk[b].pop_front();
                else cerr << "XX" << idx << ' ' << a << ' ' << b << ' ' << op << endl,assert(0);
            }
        } 
        for (int i = 1;i <= n;i++) if (!stk[i].empty()) {cerr << "NOT EMPTY" << endl;return 2;}
        if (idx != m + 1) return 3;
    }
    return 0;
}