#include #include #define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++) using namespace atcoder; using namespace std; typedef long long ll; vector> run_length_str(string &a) { int n = a.size(); vector> ret; int now = 1; rep(i, 0, n - 1) { if (a[i] == a[i + 1]) now++; else { ret.push_back(make_pair(a[i], now)); now = 1; } } ret.push_back(make_pair(a.back(), now)); return ret; } void solve() { int n; cin >> n; vector s(n), t(n); rep(i, 0, n) cin >> s[i] >> t[i]; int mx = -1; rep(i, 0, n) { if (s[i] == "a" && (t[i].find('a') != string::npos)) { cout << "Yes\n"; return; } else if (s[i] == "a") { mx = max(mx, (int)t[i].size()); } } rep(i, 0, n) { auto rls = run_length_str(s[i]); if (rls.size() == 1 && rls[0].first == 'b' && rls[0].second <= mx) { cout << "Yes\n"; return; } } cout << "No\n"; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(12); int t; cin >> t; while (t--) solve(); }