#include using namespace std; using ll = long long; template struct csr { using itr = typename std::vector::iterator; struct Node { itr st, en; itr begin() { return st; } itr end() { return en; } int size() { return en - st; } T operator[](int p){ return st[p]; } }; const int N; std::vector start; std::vector E; std::vector> edge; csr(int n) : N(n), start(n + 1) {} void add_edge(int u, T v){ assert(0 <= u && u < N); start[u + 1]++; edge.emplace_back(u, v); } void build(){ E.resize(edge.size()); for(int i = 0; i < N; i++) start[i + 1] += start[i]; auto cnt = start; for(auto [u, v] : edge) E[cnt[u]++] = v; } Node operator[](int p) { return Node{E.begin() + start[p], E.begin() + start[p + 1]}; } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); int T; cin >> T; while(T--){ int n; cin >> n; vector> b(n); vector c; c.reserve(2 * n); for(auto &&[x, y] : b){ cin >> x >> y; c.emplace_back(x); c.emplace_back(y); } sort(c.begin(), c.end()); c.erase(unique(c.begin(), c.end()), c.end()); if(c[0] != "a"){ cout << "No\n"; continue; } int m = c.size(); csr g(m); vector S; int mx = 1 << 30; for(auto [x, y] : b){ int u = lower_bound(c.begin(), c.end(), x) - c.begin(); int v = lower_bound(c.begin(), c.end(), y) - c.begin(); g.add_edge(u, v); if(x.find("a") == string::npos) mx = min(mx, (int)x.size()); } g.build(); sort(S.begin(), S.end()); S.erase(unique(S.begin(), S.end()), S.end()); auto dfs = [&](auto dfs, int v) -> bool { if(c[v].find("a") != string::npos) return true; if(c[v].size() > mx) return true; if(c[v].size() == mx){ for(auto &&u : g[v]){ if(dfs(dfs, u)) return true; } } return false; }; bool flg = false; for(auto u : g[0]){ if(dfs(dfs, u)){ flg = true; break; } } cout << (flg ? "Yes" : "No") << '\n'; } }