#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <set>
#include <cassert>
#include <map>

using namespace std;

void solve() {
    int n;
    cin >> n;
    set<string> fs;
    set<string> sc;
    vector<pair<string, string>> p;
    for (int i = 0; i < n; ++i) {
        string s, t;
        cin >> s >> t;
        fs.insert(s);
        sc.insert(t);
        p.push_back({s, t});
    }
    map <string, int> frst, scnd;
    int ind = 0;
    for (auto x: fs) {
        frst[x] = ind;
        ind++;
    }
    ind = 0;
    for (auto x: sc) {
        scnd[x] = ind;
        ind++;
    }
    vector<pair<int, int>> transformed;
    vector<int> cnt_first(n), cnt_second(n);
    for (auto x : p) {
        int a = frst[x.first];
        int b = scnd[x.second];
        transformed.push_back({a, b});
        cnt_first[a]++;
        cnt_second[b]++;
    }
    for (int i = 0; i + 1 < p.size(); ++i) {
        cnt_first[transformed[i].first]--;
        cnt_second[transformed[i].second]--;
        if (transformed[i].first != transformed[i + 1].first && transformed[i].second != transformed[i + 1].second) {
            if (cnt_first[transformed[i].first] > 0 || cnt_second[transformed[i].second] > 0) {
                cout << "No\n";
                return;
            }
        }
    }
    cout << "Yes\n";




}

signed main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}