P16399 [ECUSTPC 2026 Spring] Morning Review

Background

:::epigraph Computer Science: Review (Computer Science: Go Over, known as ___ ) is something popular around the world. :::

Description

**This problem is unrelated to Problem H "Morning Review, Evening Review".** TSUCE (Time-Space Union of Coding Experts) is going to hold the TSUCE Programming Duel Cup, a head-to-head algorithm contest between two players. The rules are as follows. Please pay attention to the meanings of $n$ and $m$: - Each match is played between two players. In each match, there is exactly one winner, and there are no draws. - A match consists of one regulation period plus several overtime periods (possibly $0$ overtime periods). The regulation period contains $2n$ rounds. Each overtime period contains $2m$ rounds. In each round, there is exactly one winner. - During regulation, the player who gets $n + 1$ round wins will win the whole match, and the match ends immediately. - If there is still no winner after the $2n$ rounds of regulation (the score is $n:n$), the match enters overtime. In a single overtime period, the player who gets $m + 1$ round wins will win the whole match, and the match ends immediately. - If there is still no winner after one overtime period, the match enters the next overtime period, until a winner is decided. - The final score is the number of rounds each side wins in the whole match. Unfortunately, scorekeeper Little T’s database broke. He only remembers the scores of all $k$ matches, but he does not remember the $n$ and $m$ related to the rules. Please help him determine whether there exists a pair of valid positive integers $n$ and $m$ such that all these scores are valid under this rule set.

Input Format

The first line contains an integer $T \ (1 \le T \le 10^5)$, the number of testdata. For each testdata, the first line contains an integer $k \ (1 \le k \le 10^5)$, the number of recorded matches. Then follow $k$ lines, each containing two integers $a$ and $b \ (0 \le a, b \le 10^{18}, |a - b| \ge 2)$, representing the score of a match recorded in Little T’s database. It is guaranteed that $\sum k \le 3 \times 10^5$ over all testdata.

Output Format

For each testdata, if there exists a valid $n$ and $m$ such that all these scores are valid under this rule set, output one line with the string YES; otherwise output one line with the string NO. Note that the judge is case-insensitive for YES and NO. In other words, if the answer is positive, outputs like yes, YES, Yes, YeS, etc. will all be accepted.

Explanation/Hint

### Sample 1 Explanation For the 3rd testdata, we can find that $n = 12, m = 3$ is a valid solution. Then the matches may go through the following process: - Match 1: $2:13$. During regulation, player B first reaches $n + 1 = 13$ wins and ends the match. - Match 2: $16:12$. Regulation ends in a $12:12$ tie. In the first overtime period, player A wins $4:0$ and gets $m + 1 = 4$ wins. - Match 3 and Match 4 end in regulation with scores $9:13$ and $13:10$, respectively. - Match 5: Regulation ends in a $12:12$ tie. In both the first and second overtime periods, the score is $3:3$, so the match reaches $18:18$ and enters the third overtime period. Finally, player B wins $4:2$ in the third overtime period, and the final score is A $20:22$ B. Translated by ChatGPT 5