#include using namespace std; struct UnionFind { vector par; vector size; UnionFind(int n) { par.resize(n); size.resize(n,1); for(int i = 0; i < n; i++) { par[i] = i; } } int find(int x) { if(par[x] == x) { return x; } return par[x] = find(par[x]); } bool same(int x, int y) { return find(x) == find(y); } int consize(int x) { return size[find(x)]; } bool unite(int x, int y) { x = find(x); y = find(y); if(x == y) { return false; } if(size[x] > size[y]) { swap(x,y); } par[x] = y; size[y] += size[x]; return true; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while(t--) { int N; cin >> N; vectorC(N); vectorD(N); UnionFind uf(2*N+3); for(int i = 0; i < N; i++) { cin >> C[i] >> D[i]; D[i]--; for(int j = 0; j < 3; j++) { if("RGB"[j] == C[i]) uf.unite(N+j,i); } uf.unite(N+3+D[i],i); } setst; for(int i = 0; i < N; i++) { st.insert(uf.find(i)); } cout << ((st.size() == 1)?"YES":"NO") << "\n"; } }