#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // #define _GLIBCXX_DEBUG // #define _LIBCPP_DEBUG 0 #define _GLIBCXX_DEQUE_BUF_SIZE 64 #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define revrep(i, n) for(int i = (int)(n - 1); i >= 0; --i) #define range(i, s, t) for (int i = (int)(s); i < (int)(t); ++i) #define revrange(i, s, t) for(int i = (int)(t - 1); i >= (int)(s); --i) #define srange(i, s, t, step) for(int i = (int)(s); i < (int)(t); i+=int(step)) #define popcnt(x) __builtin_popcountll(x) #define hayai std::ios::sync_with_stdio(false);std::cin.tie(nullptr) #define SORT(x) sort(x.begin(), x.end()) #define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end()) using namespace std; using ll = long long; using pii = pair; using pll = pair; template inline bool chmax(T& a, T b){ if (a < b){ a = b; return true; } return false; } template inline bool chmin(T& a, T b){ if (a > b){ a = b; return true; } return false; } inline bool inside(int x, int y, int h, int w) { return (x >= 0 && x < h && y >= 0 && y < w); } constexpr pair dydx4[] = { {0, 1}, {-1, 0}, {0, -1}, {1, 0}, }; //RULD constexpr pair dydx8[] = { {0, 1},{-1, 1},{-1, 0},{-1, -1},{0, -1},{1, -1},{1, 0},{1, 1}, }; struct union_find{ public: union_find(int size): _n(size), root(size, -1) {} int find(int x){ if (root[x] < 0) return x; root[x] = find(root[x]); return root[x]; } bool same(int x, int y){ return find(x) == find(y); } bool merge(int x, int y){ x = find(x); y = find(y); if (x == y) return false; if (-root[x] < -root[y]) swap(x, y); root[x] += root[y]; root[y] = x; return true; } int size(int x){ return -root[find(x)]; } private: int _n; vector root; }; void solve() { int n; cin >> n; union_find uf(26 + n + n); rep(i, n) { char c; int d; cin >> c >> d; d--; uf.merge(c - 'A', 26 + n + i); uf.merge(26 + d, 26 + n + i); } int leader = uf.find(26 + n); rep(i, n) { int l = uf.find(26 + n + i); if(l != leader) { cout << "NO" << "\n"; return; } } cout << "YES" << "\n"; } int main() { hayai; int t; cin >> t; rep(i, t) solve(); }