#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define pb push_back #define mp make_pair #define pii pair #define vi vector #define vpii vector #define SZ(x) ((int)(x.size())) #define fi first #define se second #define FOR(i,n) for(int (i)=0;(i)<(n);++(i)) #define FORI(i,n) for(int (i)=1;(i)<=(n);++(i)) #define IN(x,y) ((y).find((x))!=(y).end()) #define ALL(t) t.begin(),t.end() #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i) #define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i) #define REMAX(a,b) (a)=max((a),(b)); #define REMIN(a,b) (a)=min((a),(b)); #define DBG cerr << "debug here" << endl; #define DBGV(vari) cerr << #vari<< " = "<< (vari) < mem; int seq_len; int n; bool solve(int pos, vi& state) { if(pos == seq_len) { FOR(i, n) if(state[i] > 0) return 0; return 1; } if(mem.find(state) == mem.end()) { bool res = 0; FOR(i, n) { if(state[i] > 0 && b[i][state[i] - 1] == seq[pos]) { state[i]--; bool subres = solve(pos + 1, state); state[i]++; if(subres) { res = 1; break; } } } mem.insert(mp(state, res)); } return mem.find(state)->se; } int main() { ios_base::sync_with_stdio(0); int t; cin >> t; assert(t >= 1 && t <= T); while(t--) { cin >> n; assert(n >= 1 && n <= N); int p = 1; seq_len = 0; FOR(i, n) { cin >> a[i]; seq_len += a[i]; p *= (a[i] + 1); FOR(j, a[i]) { int bi; cin >> bi; assert(bi >= 1 && bi <= B); b[i][j] = bi; } } assert(p >= 1 && p <= MAX_TOTAL_P); FOR(i, seq_len) cin >> seq[i]; if(n == 1) { bool fail = 0; for(int i = 0; i < seq_len; ++i) { if(seq[i] != b[0][seq_len - 1 - i]) fail = 1; } cout << (!fail ? "Yes" : "No") << endl; continue; } vi initial_state; FOR(i, n) initial_state.pb(a[i]); bool res = solve(0, initial_state); cout << (res ? "Yes" : "No") << endl; } return 0; }