Hide

Problem H
Hierarchy Hassle

You work in a company with a tremendously intricate hierarchy. Every employee has exactly one manager, and following any managerial relation chain up the hierarchy leads to the boss of the company. In this company, every employee has a backlog of emails they must process, meaning there is a delay before they can act on any new request. Today we are interested in how disputes are resolved in the company.

If a dispute arises in the company between two employees, they will both immediately report it to their own manager. The managers will then report it to their own managers when they have processed their backlog, and so on, until the dispute reaches a common manager. The common manager is the manager lowest in the hierarchy to oversee both disputing employees. Unfortunately for the employees, the current policy on how to resolve disputes states that their common manager must always favor the employee whose email arrives first. Since every employee has a backlog of emails to get through, the time it takes for an email to reach a manager will depend on the backlogs of all employees along the path from the disputing employee to the common manager. If the emails from both employees arrive at the same time, the policy states that the manager must favor the employee which is closest to being promoted to CEO. This means having the lowest number of intermediate managers between themselves and the current CEO. Therefore, only when disputing employees are equally close to being promoted to CEO, and their complaints reach the lowest common boss simultaneously does the manager have to sit down and actually consider who to support in the dispute. Disputes where one employee is directly or indirectly the manager of the other employee will be resolved instantly.

Because of this policy, it is rarely worthwhile to report a dispute, because the outcome is typically predetermined. You are a part of the human resources department that helps people know the outcome of their disputes before they report them. Given your knowledge of the company hierarchy and the backlogs of all employees, you will be provided with pairs of employees and determine who will be favored in the dispute.

Input

Each employee has a unique integer ID from $1$ to $N$, where $1$ is the boss of the company.

The first line contains $N$, an integer denoting the number of employees in the company. Then follows $N-1$ lines, where the $i$’th line contains two integers $m_i$ and $d_i$, denoting that employee $i+1$ reports to manager $m_i$, and that it takes $d_i$ time to get through employee $i+1$’s backlog of emails. Note that the boss of company does not have a backlog of emails, because company bosses are simply better people.

Then follows a line containing $T$, the number of disputes that arise in the company. Then follows $T$ lines, where each line contains two integers $a_t$ and $b_t$, denoting a dispute between employees $a_t$ and $b_t$.

Output

For each dispute, output the ID of the employee who will be favored in the dispute, and the time it takes for the email of the favored employee to reach the common manager. If no employee is favored, output “No one”.

Limits

  • $1 \leq m_{i}, a_t, b_t \leq N \leq 100000$

  • $1 \leq T \leq 50000$

  • $1 \leq d_{i} \leq 1000$

Sample Input 1 Sample Output 1
6
1 2
1 3
2 3
3 2
2 2
4
2 3
4 5
6 3
1 2
2 2
No one
3 3
1 0

Please log in to submit a solution to this problem

Log in