Polycarp has recently created a new level in this cool new game Berlio Maker 85 and uploaded it online. Now players from all over the world can try his level.
All levels in this game have two stats to them: the number of plays and the number of clears. So when a player attempts the level, the number of plays increases by 1. If he manages to finish the level successfully then the number of clears increases by 1 as well. Note that both of the statistics update at the same time (so if the player finishes the level successfully then the number of plays will increase at the same time as the number of clears).
Polycarp is very excited about his level, so he keeps peeking at the stats to know how hard his level turns out to be.
So he peeked at the stats n times and wrote down n pairs of integers — (p_1, c_1), (p_2, c_2), ..., (p_n, c_n), where p_i is the number of plays at the i-th moment of time and c_i is the number of clears at the same moment of time. The stats are given in chronological order (i.e. the order of given pairs is exactly the same as Polycarp has written down).
Between two consecutive moments of time Polycarp peeked at the stats many players (but possibly zero) could attempt the level.
Finally, Polycarp wonders if he hasn't messed up any records and all the pairs are correct. If there could exist such a sequence of plays (and clears, respectively) that the stats were exactly as Polycarp has written down, then he considers his records correct.
Help him to check the correctness of his records.
For your convenience you have to answer multiple independent test cases.
Input
The first line contains a single integer T (1 ≤ T ≤ 500) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 100) — the number of moments of time Polycarp peeked at the stats.
Each of the next n lines contains two integers p_i and c_i (0 ≤ p_i, c_i ≤ 1000) — the number of plays and the number of clears of the level at the i-th moment of time.
Note that the stats are given in chronological order.
Output
For each test case print a single line.
If there could exist such a sequence of plays (and clears, respectively) that the stats were exactly as Polycarp has written down, then print "YES".
Otherwise, print "NO".
You can print each letter in any case (upper or lower).
In the third test case the number of plays decreased, which is impossible.
The fourth test case is probably an auto level with a single jump over the spike.
In the fifth test case the number of clears decreased, which is also impossible.
Nobody wanted to play the sixth test case; Polycarp's mom attempted it to make him feel better, however, she couldn't clear it.
We need to check if there exists a sequence of play attempts between observation times that can result in the given (p_i, c_i) pairs in chronological order, given that p is total plays and c is total clears, both non-decreasing? Actually the problem statement says: When a player attempts the level, plays increase by 1. If he finishes successfully, clears increase by 1 as well. Both stats update at the same time (so if the player finishes, both p and c increase simultaneously). So p and c are non-decreasing? They are both monotonically non-decreasing as time passes, because plays and clears can only increase (or stay same) as attempts happen. However note that p_i and c_i can also remain same if no attempts between observations. But can p ever decrease? No. So condition: p_i ≥ p_{i-1} and c_i ≥ c_{i-1} for all i>1. Also, at any time, the number of clears cannot exceed the number of plays: c_i ≤ p_i. Also, the increment in clears cannot exceed the increment in plays between observations because each successful attempt increases both by 1, and each unsuccessful attempt increases only p by 1. So if we let Δp = p_i - p_{i-1} and Δc = c_i - c_{i-1}, we must have Δp ≥ Δc ≥ 0. Also the starting point must be consistent: first observation (p_1, c_1) must satisfy c_1 ≤ p_1 and likely p_1,c_1 >=0.
But we must also consider that between observations, many attempts happen, and the stats update after each attempt. The recorded pairs are at specific moments after some number of attempts. Since the increments are integer, the conditions above are necessary. Are they sufficient? Let's check: Given that p and c are non-decreasing and Δp >= Δc >= 0, can we always find a sequence of attempts that results in exactly those increments? Yes: we can imagine that between observation i-1 and i, there were Δc successful attempts and (Δp - Δc) failed attempts. Order doesn't matter because stats update simultaneously, but they both increase appropriately. However is there any other constraint like the stats update at the same time for each attempt, so the intermediate states could be something but final numbers are as given. There's no requirement that the stats at intermediate times match some pattern; only that it's possible. So the conditions above seem sufficient.
But wait, there might be a subtlety: The records are at specific moments. Could there be a situation where Δp and Δc satisfy Δp ≥ Δc but the records are still impossible? For example, suppose we have p_1=0, c_1=0 and p_2=1, c_2=0 is possible (one failed attempt). p_3=1, c_3=1 would be impossible because from 1,0 to 1,1 would require Δp=0, Δc=1, which violates Δp >= Δc. But that's covered. Another nuance: Could there be a scenario where the records show that c increases more than p relative to a previous time? Already covered.
What about the initial values? For the first pair, c_1 must be <= p_1, and also they must be achievable from the start (before any attempts). At time zero, presumably p=0, c=0. But the problem statement says "Polycarp peeked at the stats n times and wrote down n pairs ... at the i-th moment of time." The first peek is at some moment after possibly some attempts have already happened. So the first pair could be any numbers that are achievable from the start (0,0) by some sequence of attempts. That means p_1 ≥ 0, c_1 ≥ 0, and c_1 ≤ p_1, but also increments from (0,0) to (p_1, c_1) must satisfy p_1 >= c_1 >=0? Actually we can treat the initial state before any attempts as (0,0). So for the first observation, we need to check that (p_1, c_1) is reachable from (0,0). That requires p_1 >= c_1 and both nonnegative. But also p_1 and c_1 could be any nonnegative integers with c_1 ≤ p_1; that's reachable by having c_1 successful attempts and (p_1 - c_1) failures. So first pair must have c_1 ≤ p_1.
Now, what about cases where p_i and c_i both increase but c_i - c_{i-1} > p_i - p_{i-1}? That would be impossible because each successful attempt increases both p and c by 1, so the increase in c cannot exceed increase in p. Also, we must have p_i >= p_{i-1} and c_i >= c_{i-1}, because plays and clears never decrease. Also c_i <= p_i must hold at each step (since clears cannot exceed plays). But is c_i <= p_i automatically ensured by the previous conditions if it holds at the start? Possibly not: Suppose p_1=10, c_1=5; then p_2=11, c_2=10? Δp=1, Δc=5? That fails Δp>=Δc. But consider scenario where p_2=15, c_2=15? Then Δp=5, Δc=10? fails. But there could be a scenario where Δp and Δc are okay but c_i > p_i? For that to happen, we need c_{i-1} ≤ p_{i-1}, and Δp >= Δc, then c_i = c_{i-1} + Δc ≤ p_{i-1} + Δp = p_i. So if c_{i-1} ≤ p_{i-1} and Δp >= Δc, then c_i ≤ p_i. So it's sufficient to enforce initial condition c_1 ≤ p_1 and for each step Δp >= Δc >=0 and p_i >= p_{i-1} and c_i >= c_{i-1}. Actually Δc >=0 ensures c_i >= c_{i-1}, and Δp >= Δc ensures p_i >= p_{i-1} (since Δp≥0? Actually Δp could be negative? p_i must not decrease, so we need Δp >=0 as well. But Δp>=Δc ensures Δp >=0 only if Δc>=0. But it's possible that Δc=0 but Δp could be negative? That would violate monotonicity, but our condition Δp >= Δc would allow negative Δp if Δc is negative? But we also enforce c_i >= c_{i-1} so Δc >=0. So we should enforce both Δp >=0 and Δc >=0 as part of monotonicity. But Δp >= Δc with Δc>=0 implies Δp >=0 anyway. However we need to also ensure p_i >= p_{i-1} explicitly? Suppose Δp = -1, Δc = -2, then Δp >= Δc holds (since -1 >= -2) but both negative; however we must not allow p to decrease. So we need Δp >= 0 and Δc >= 0. So we need: p_i >= p_{i-1}, c_i >= c_{i-1}, and p_i - p_{i-1} >= c_i - c_{i-1} . Also initial: p_1 >= 0, c_1 >=0, c_1 <= p_1. Actually p_1 and c_1 given could be zero or positive, but they must be reachable from (0,0) so p_1 >= c_1 and both >=0.
1 0, 1000 3
First: 0<=1 ok. Second: Δp=999, Δc=3, 999>=3, p increase, c increase, ok => YES.
10 1, 15 2, 10 2, 15 2
Check: first: 1<=10 ok. Second: Δp=5, Δc=1 ok. Third: p3=10, p2=15 => Δp = -5 (decrease) -> violates p non-decreasing => NO. So matches.
765 432
Only one pair: check 432 <= 765 => YES. (since it's reachable from start by 432 successes and 333 failures). Good.
4 4, 4 3
First: 4<=4 ok. Second: p2=4 >= p1=4 ok, c2=3 < c1=4 => c decreased, not allowed => NO.
0 0, 1 0, 1 0, 1 0, 1 0
First: ok. Second: Δp=1, Δc=0 ok. Third: Δp=0, Δc=0 ok (no change). Fourth: same. Fifth: same. All good => YES.
So the necessary and sufficient conditions appear to be as described.
But is there any other subtlety? For instance, could there be a scenario where the recorded p_i and c_i are such that at some point the total number of clears is more than total plays? Already covered. Could there be a scenario where a play attempt increases both stats simultaneously, but the recorded stats could show an increase in p without an increase in c? That's allowed, as failures increase only p.
One more thing: The stats update at the same time for a successful attempt: both increase together. So you cannot have a situation where a successful attempt increments c without incrementing p? That's not possible because a successful attempt is still an attempt, so p increments too. That's covered by Δp >= Δc.
Thus the solution: For each test case, read n, then pairs. Check:
For i=1: p_i >= 0, c_i >= 0 (implicitly given) and c_i <= p_i.
For i from 2 to n:
p_i >= p_{i-1}
c_i >= c_{i-1}
p_i - p_{i-1} >= c_i - c_{i-1}
If all hold, print YES else NO.
Complexity O(n) per test.
Edge cases: n=1, just check c1 <= p1.
Constraints: T up to 500, n up to 100, values up to 1000, so easy.
Let's consider if any other condition is needed: Since the stats are updated after each attempt, it's possible that the sequence of attempts between observations could produce the exact increments. That is straightforward. So conditions are sufficient.
Wait: Could there be a scenario where p_i and c_i are both non-decreasing, Δp >= Δc, but still impossible because of something like you cannot have a successful attempt without a play attempt? That's exactly Δp >= Δc. There's no other hidden constraint.
Therefore answer is as above.
Now I'll produce final solution.
Implementation: For each test case:
Read n.
Initialize prev_p = 0, prev_c = 0? Actually the initial state before any attempts is (0,0). But careful: The first observation is after some attempts. So we must check that the first pair is reachable from (0,0). So we can set prev_p = 0, prev_c = 0 before reading any, then for each observation in order, treat it as the next state. That is simpler: start with p_prev = 0, c_prev = 0. For each of the n observations, read p, c. Compute Δp = p - p_prev, Δc = c - c_prev. Check conditions: Δp >= 0, Δc >= 0, and Δp >= Δc. Also ensure p >= c? Actually if we enforce Δp >= Δc and initial condition p_prev>=c_prev (with p_prev=0,c_prev=0), then p >= c automatically, but it's safer to also check p >= c because if initial condition is not satisfied? But initial is fine. Actually if we do this incremental check starting from (0,0), we don't need an explicit check for p_i >= c_i because it's implied: p_i = p_prev + Δp, c_i = c_prev + Δc, with p_prev >= c_prev and Δp >= Δc, then p_i >= c_i. So it's fine.
But note: Is it allowed that the first observation has p=0, c=0? That's fine. So we can just start from zero.
One nuance: What if the records show that at some observation, p and c are both zero, but later p and c become zero again? That's fine because p non-decreasing, so p cannot go back to zero if it increased, but if it stayed zero, it's fine. So starting from zero is fine.
Thus algorithm:
For each test case:
read n
valid = True
prev_p = 0; prev_c = 0
for i in 1..n:
read p, c
if p < prev_p or c < prev_c or p - prev_p < c - prev_c:
valid = False
prev_p = p; prev_c = c
print("YES" if valid else "NO")
But careful: The condition p < prev_p or c < prev_c ensures non-decreasing. And then also need Δp >= Δc which is p-prev_p >= c-prev_c. However, if p < prev_p, then p-prev_p is negative, and c-prev_c might also be negative, but we already flagged invalid due to p<prev_p. So it's fine.
Edge: Could p stay same and c increase? That gives Δp=0, Δc>0, fails Δp>=Δc => invalid. So good.
One more: The problem statement says the stats are (p_i, c_i) with 0 ≤ p_i, c_i ≤ 1000. So they are nonnegative. So we don't need extra checks.
Now let's verify with sample: Test case 2: prev=(0,0). First (1,0): p>=0, c>=0, Δp=1>=0, Δc=0, Δp>=Δc holds. Then next (1000,3): p=1000>=1, c=3>=0, Δp=999, Δc=3, 999>=3. So YES.
Test case 3: (10,1): OK. Then (15,2): OK. Then (10,2): p<15 => invalid. So NO.
Test case 5: (4,4): OK. Then (4,3): c<4 => invalid.
Test case 6: (0,0): OK. (1,0): OK. (1,0): p>=1 ok, c>=0 ok, Δp=0, Δc=0 ok. Similarly all others.
Looks good.
Now consider potential tricky cases: Suppose records: (2,0), (2,1). Starting from (0,0) to (2,0): Δp=2, Δc=0, valid. Next: p=2, c=1: p same, c increased by 1 => Δp=0, Δc=1 => invalid. So NO. This matches reasoning.
What about (2,2), (3,2)? Δp=1, Δc=0, valid. That's possible: after having 2 successes and 0 fails (so p=2, c=2), then one failure: p becomes 3, c stays 2. So YES.
What about (0,0), (0,0) many times? valid.
Now any scenario where starting from zero, we have p_i and c_i such that c_i > p_i but the incremental check might not catch if we check only Δ? But if we start from (0,0) and enforce each step Δp>=Δc and non-decrease, then by induction p_i >= c_i. For the first step, p1 >= c1 because Δp1 >= Δc1 and both start zero. So it's guaranteed. So we don't need explicit p_i >= c_i check. However, if the first observation had c1 > p1, then Δp1 = p1, Δc1 = c1, and p1 >=0? Actually p1 >= 0? Yes but we check Δp>=Δc? That would be p1 >= c1, which would fail if c1>p1. So it's caught.
Thus the solution is straightforward.
But let's read the problem statement again: "Between two consecutive moments of time Polycarp peeked at the stats many players (but possibly zero) could attempt the level." So it's allowed that no attempts happen, which yields Δp=0, Δc=0. So okay.
All good.
Now, sometimes people think about the possibility of the stats updating at the same time meaning that for a successful attempt, both numbers increase simultaneously, so at any moment the numbers must satisfy c <= p, and the difference p-c is the number of failed attempts. The difference should be non-decreasing? Actually p-c is the number of fails (since p = successes + fails, c = successes). Does p-c ever decrease? It could if a failed attempt later becomes a success? But no, because once a player fails, that's it; we don't convert fails to successes. So p-c (fails) is also non-decreasing? When a player fails, p increases, c stays same, so fails increase by 1. When a player succeeds, both p and c increase by 1, so fails stay same. So fails never decrease; they are non-decreasing. That's an equivalent condition: p_i - c_i is non-decreasing. And also p_i and c_i non-decreasing. And initial fails 0. So check p_i - c_i >= previous p_{i-1} - c_{i-1}? But that is equivalent to Δp - Δc >= 0, which we already have. And also p_i >= c_i (non-negative fails). So we could also use that.
But our incremental approach is fine.
Now let's think about the problem: maybe there is an additional subtlety: The records are "exactly as Polycarp has written down". Could there be a scenario where the stats at some moment are (p_i, c_i) but it's impossible because the number of clears cannot exceed the number of plays at any intermediate point, but between observations we don't see intermediate points, but the process must be such that after each attempt, the stats always satisfy c <= p. However, if we design the sequence of attempts between observations (like we decide order of successes and failures) we can ensure that after each attempt the condition holds, because we start from a valid state (p_{i-1}, c_{i-1}) which satisfies c_{i-1} <= p_{i-1}, and then each attempt either increases p and c together or p alone. In either case, after each attempt the invariant c <= p remains (since if c <= p before, adding 1 to both preserves it; adding 1 to p only also preserves). So any order works. So no issues.
Thus solution is trivial.
One more check: The problem states "Note that both of the statistics update at the same time (so if the player finishes the level successfully then the number of plays will increase at the same time as the number of clears)." That just emphasizes they increment simultaneously, not that the plays increment first then clears; they both increment together. So it doesn't affect constraints.
Alright.
Now I'll produce final answer in Python.
Implementation details:
Read integer T. Loop T times. For each, read n. Then loop n times, reading p, c. Maintain prev_p, prev_c initially 0. For each pair, compute dp = p - prev_p, dc = c - prev_c. If dp < 0 or dc < 0 or dp < dc: set flag false. After reading all, print accordingly.
But careful: The input may have extra spaces, but that's fine.
Now test on provided sample.
We'll run mental test.
Case 1: n=3: pairs: (0,0): dp=0, dc=0 -> ok; (1,1): dp=1, dc=1 -> ok; (1,2): dp=0, dc=1 -> dp < dc => NO. So output NO.
Case 2: n=2: (1,0): dp=1, dc=0 ok; (1000,3): dp=999, dc=3, ok -> YES.
Case 6: n=5: (0,0): ok; (1,0): dp=1,dc=0 ok; (1,0): dp=0,dc=0 ok; (1,0): ok; (1,0): ok -> YES.
Matches sample.
Thus solution done.
Now we could also incorporate the p_i >= c_i check after each reading for safety but not needed.
Now I'll produce final answer.
But wait, let's consider a possible edge case: n=0? Not allowed, n>=1.
One more scenario: The records might show p_i and c_i such that at some point c_i > p_i even though increments okay? But we argued induction ensures not. But suppose first observation is (1,1) from (0,0): dp=1,dc=1 -> ok. Second observation (2,3)? dp=1,dc=2 -> fails dp>=dc. So caught. If we had (2,2) then (3,3) then (4,3)? dp=1,dc=0 ok. So fine.
Now, is there any scenario where dp and dc satisfy dp>=dc and non-negative, but at the same time the absolute values cause an inconsistency with the total number of fails? For example, suppose we have (p1,c1) = (5,2) so fails=3. Then (p2,c2) = (6,5) gives dp=1, dc=3? Actually dc = 5-2 = 3, dp=1, so fails condition: p2-c2 = 1, which is less than previous fails 3. So fails decreased: that would be caught by dp < dc? Check: dp=1, dc=3 => 1>=3 false, so invalid. So that's caught.
What about scenario where dp and dc are both zero, but then later p increases but c doesn't? That's allowed.
Thus the three conditions are complete.
I'll write solution.
Potential pitfalls: The input might have negative numbers? No, given constraints 0 ≤ p_i, c_i ≤ 1000. So no negative.
Now finalize.
But before concluding, let's confirm the reasoning with the problem's note: "In the first test case at the third moment of time the number of clears increased but the number of plays did not, that couldn't have happened." That's exactly our check. "In the third test case the number of plays decreased, which is impossible." Yes, we check p_i >= p_{i-1}. "The fifth test case the number of clears decreased, which is also impossible." Yes, c_i >= c_{i-1}.
Thus solution accepted.
Now produce final answer in Python.
Given T up to 500, n up to 100, trivial.
I'll include fast I/O just in case.
Write:
import sys
def solve():
data = sys.stdin.read().strip().split()
it = iter(data)
T = int(next(it))
out = []
for _ in range(T):
n = int(next(it))
valid = True
prev_p = 0
prev_c = 0
for i in range(n):
p = int(next(it)); c = int(next(it))
if not valid:
continue # skip reading? But we already read, so just compute
dp = p - prev_p
dc = c - prev_c
if dp < 0 or dc < 0 or dp < dc:
valid = False
prev_p = p
prev_c = c
out.append("YES" if valid else "NO")
sys.stdout.write("\n".join(out))
if name == "main":
solve()
That's it.
Check: For sample input, output should match.
Now consider if there is any scenario where the initial prev_p=0, prev_c=0 is not appropriate? Could the first observation be before any attempts? Yes, but if it's after some attempts, that's fine. If it's before any attempts, then (0,0) is the only possibility, but the first observation could be (0,0) if he peeked before any attempts. That's still valid.
Thus solution is correct.
Now I'll provide final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
it = iter(data)
T = int(next(it))
answers = []
code
for _ in range(T):
n = int(next(it))
valid = True
prev_p = 0
prev_c = 0
for _ in range(n):
p = int(next(it))
c = int(next(it))
dp = p - prev_p
dc = c - prev_c
if dp < 0 or dc < 0 or dp < dc:
valid = False
prev_p = p
prev_c = c
answers.append("YES" if valid else "NO")
sys.stdout.write("\n".join(answers))