Three Competitions

아직 제출이 없습니다시간 제한5초메모리 제한1024 MB

문제

Last month, nn people participated in three competitions. The people are labeled with distinct integers from 11 to nn. In each competition, the people were sorted by performance and got ranked from 11 to nn. The lower the rank, the better the player. There were no ties in any of the rankings.

Today, instead of nn people participating at once, two people compete head-to-head. The winner of the match is the person who wins at least two out of three competitions. The winner then proceeds to compete with another person. This turned out to be quite interesting: even if a person aa cannot directly win against another person bb, it's possible that another person cc wins against bb and then aa wins against cc. That way, we can say that aa "indirectly" wins against bb. It's also possible that two people can indirectly win against each other!

Formally, a person aa is said to directly win against another person bb if aa has a lower rank than bb in at least two competitions. Also, aa is said to indirectly win against bb if there exists a sequence of people p_1,p_2,,p_kp\_1, p\_2, \cdots, p\_k (k2k \geq 2) such that p_ip\_i directly wins against p_i+1p\_{i+1} for all i=1,,k1i = 1, \cdots, k-1, p_1=ap\_1 = a and p_k=bp\_k = b.

Given the ranks of the people in each competition, answer qq questions asking whether person aa indirectly wins against another person bb.

입력

The first line contains a single integer n (2n2105)n\ (2 \leq n \leq 2 \cdot 10^5), the number of people.

Each of the next nn lines contains three integers, which represent the ranks of each person in each of the three competitions, in order from person 11 to person nn. For each competition, each integer rank from 11 to nn appears exactly once.

The next line contains a single integer q (1q2105)q\ (1 \leq q \leq 2 \cdot 10^5), the number of questions.

Each of the next qq lines contains two integers aa and b (1a,bnb\ (1 \le a, b \le n, ab)a \neq b), asking whether person aa indirectly wins against person bb.

출력

Output qq lines. The ii-th line should be either \verb YES \ or \verb NO . If person aa indirectly wins against person bb, output \verb YES , otherwise output \verb NO .

힌트

Person 1 directly (and indirectly) wins against 2. Person 2 doesn't directly win against 1, but 2 directly wins against 3 and 3 directly wins against 1, so 2 indirectly wins against 1.