#P14609. [NWRRC 2025] Judging Problem

[NWRRC 2025] Judging Problem

题目描述

The judges of NWRRC came up with nn problems on a similar topic and decided to use them for nn consecutive years, one problem per year. The only question was: in what order should they be used?

Each problem's name consists of two words. Let's call two names similar\textit{similar} if either their first words are the same or their second words are the same. For example, eight shaped\texttt{eight shaped} and eight connected\texttt{eight connected} are similar, while hello world\texttt{hello world} and world hello\texttt{world hello} are not.

The judges decided to implement the following rule: in the first year, they chose a problem arbitrarily. In every subsequent year, if there was a problem with a name similar to the previous year's problem that was still unused, they chose one of such problems; otherwise, they chose any unused problem.

You are given the names of the problems in chronological order of their use. Determine whether the judges correctly followed the rule above, or if they made a mistake.

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn, denoting the number of problems (2n1052 \le n \le 10^5).

The ii-th of the following nn lines contains the name of the ii-th problem in chronological order: two words consisting of at least 11 and at most 1010 lowercase English letters each. All problem names are distinct.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

输出格式

For each test case, print Yes\tt{Yes} if the judges followed the rule correctly, and No\tt{No} otherwise.

3
4
k shaped
h shaped
eight shaped
eight connected
3
k shaped
eight connected
eight shaped
4
judging problem
judging logic
binary problem
logic problem
Yes
No
Yes

提示

In the first test case, each subsequent problem name is similar to the previous one.

In the second test case, the judges should have chosen eight shaped\tt{eight\ shaped} for the second year.

In the third test case, neither binary problem\tt{binary\ problem} nor logic problem\tt{logic\ problem} is similar to judging logic\tt{judging\ logic}; the judges could have chosen either of those problems for the third year.