QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 512 MB Total points: 100

#18805. 트리와 쿼리 2

Statistics

There is a tree (undirected connected graph without cycles) consisting of $N$ vertices. The vertices are numbered from $1$ to $N$, and the edges are numbered from $1$ to $N-1$.

Write a program that performs the following two types of queries:

  • 1 u v: Output the cost of the path from $u$ to $v$.
  • 2 u v k: Output the $k$-th vertex on the path from $u$ to $v$. Here, $k$ is less than or equal to the number of vertices on the path from $u$ to $v$.

Input

The first line contains $N$ ($2 \le N \le 100\,000$).

The next $N-1$ lines each contain two vertex numbers $u$ and $v$ connected by edge $i$, and the cost $w$ of that edge.

The next line contains the number of queries $M$ ($1 \le M \le 100\,000$).

The next $M$ lines each contain a query.

The cost of an edge is always a natural number less than or equal to $1\,000\,000$.

Output

For each query, output the result on a separate line in order.

Examples

Input 1

6
1 2 1
2 4 1
2 5 2
1 3 1
3 6 2
2
1 4 6
2 4 6 4

Output 1

5
3

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.