This page is still under construction.

Parts of this page are still being built. What you see may change.

Three Competitions

Time limit5sMemory limit1024 MB

Summary
Each person has ranks in three competitions; a directly wins against b if a beats b in at least two, and queries ask whether a reaches b through such direct wins.
Level

Hard8 of 10

Topics
Graph, Sorting, DFS, Two pointers
Solved
No attempts yet

Problem

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 (k≥2k \geq 2) such that p_ip\_i directly wins against p_i+1p\_{i+1} for all i=1,⋯ ,k−1i = 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.

Input

The first line contains a single integer n (2≤n≤2⋅105)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 (1≤q≤2⋅105)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 (1≤a,b≤nb\ (1 \le a, b \le n, a≠b)a \neq b), asking whether person aa indirectly wins against person bb.

Output

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 .

Hint

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.

Examples1

  1. Example 1

    Input
    4
    2 4 3
    3 1 4
    4 3 2
    1 2 1
    3
    1 2
    2 1
    3 4
    
    Expected output
    YES
    YES
    NO