#include <bits/stdc++.h>

using ll = long long;

void solveTest() {
    int n;
    std::cin >> n;
    std::vector<std::pair<std::string, std::string>> names(n);
    std::unordered_map<std::string, int> lastByFirst, lastBySecond;
    for (int i = 0; i < n; ++i) {
        std::cin >> names[i].first >> names[i].second;
        lastByFirst[names[i].first] = i;
        lastBySecond[names[i].second] = i;
    }
    for (int i = 1; i < n; ++i) {
        auto[x, y] = names[i];
        auto[px, py] = names[i - 1];

        if (x != px && y != py && (lastByFirst[px] > i || lastBySecond[py] > i)) {
            std::cout << "No\n";
            return;
        }
    }

    std::cout << "Yes\n";
}

int main() {
    int t;
    std::cin >> t;
    while (t--) {
        solveTest();
    }
    
    return 0;
}
