#include "bits/stdc++.h"
using namespace std;

#define rep(i,a,b) for(int i = (a); i < (b); ++i)
#define all(x) begin(x),end(x)
#define sz(x) int(x.size())
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long ll;

int main(){
    cin.tie(0)->sync_with_stdio(0);
    
    int t; cin >> t;
    while(t--){
        int n; cin >> n;

        vector<array<string,2>> a(n);
        for(auto& [x,y] : a) cin >> x >> y;
        
        map<string,multiset<string>> fts, stf;
        rep(i,1,n){
            auto [x,y] = a[i];
            fts[x].insert(y);
            stf[y].insert(x);
        }

        bool good = 1;

        array<string,2> lst = a[0];
        rep(i,1,n){
            auto [x,y] = a[i];

            fts[x].erase(fts[x].find(y));
            stf[y].erase(stf[y].find(x));

            if (x == lst[0] or y == lst[1]){

            }else{
                if (fts[lst[0]].size() or stf[lst[1]].size()){
                    good = 0;
                    break;
                }
            }

            lst = a[i];

        }


        if (good) cout << "YES\n";
        else cout << "NO\n";
    }
}