#ifndef __APPLE__
#include <bits/stdc++.h>
#else
#include <iostream>
#include <vector>
#include <set>
#include "map"
#include "iomanip"
#include "cassert"
#include "numbers"
#include "random"
#endif
using namespace std;
#define all(x) x.begin(), x.end()
#define int long long

const int nm = 1000007;


void solve() {
    int k;
    cin >> k;
    vector<string> a(k);
    vector<string> b(k);
    map<string, int> m;
    for (int i = 0; i < k; ++i) {
        cin >> a[i] >> b[i];
        m[a[i]] = 0;
        m[b[i]] = 0;
    }
    int sigma=0;
    for (auto &i : m)
        i.second = sigma++;
    map<int, int> mf, ms;
    for (int i = 0; i < k; ++i) {
        mf[m[a[i]]]++;
        ms[m[b[i]]]++;
    }
    bool res = 1;
    for (int i = 0; i + 1 < k; ++i) {
        bool ok = 0;
        if (a[i] == a[i+1] or b[i] == b[i + 1])
            ok = 1;
        mf[m[a[i]]]--;
        ms[m[b[i]]]--;
        if (!ok) {
            if (mf[m[a[i]]] or ms[m[b[i]]]) {
                res = 0;
            }
        }
    }
    if (res)
        cout << "Yes\n";
    else
        cout << "No\n";
}

signed main() {
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);

    int t = 1;
    cin >> t;
    while (t--)
        solve();
}
/*

 */