#C5487. Safety Paths in Treehouses

    ID: 49141 Type: Default 1000ms 256MiB

Safety Paths in Treehouses

Safety Paths in Treehouses

You are given a configuration of treehouses connected by bridges. Each bridge has a safety score. The treehouses form a tree structure such that there is exactly one simple path between any two treehouses.

For each query, you need to compute the total safety score along the unique path between the two queried treehouses. Mathematically, if the path from treehouse \(A\) to treehouse \(B\) uses bridges with safety scores \(s_1, s_2, \ldots, s_k\), then you must output:

[ S = s_1 + s_2 + \cdots + s_k ]

Note: It is guaranteed that the graph of treehouses is connected and forms a tree.

inputFormat

The input is read from stdin and has the following format:

  • The first line contains an integer \(N\) representing the number of treehouses.
  • The next \(N-1\) lines each contain three integers \(U\), \(V\), and \(S\) indicating a bridge between treehouse \(U\) and treehouse \(V\) with safety score \(S\).
  • The following line contains an integer \(Q\), the number of queries.
  • The next \(Q\) lines each contain two integers \(A\) and \(B\) representing a query to determine the total safety score along the path between treehouses \(A\) and \(B\).

outputFormat

For each query, output a single line containing the total safety score of the unique path between the two treehouses. The results should be printed to stdout.

## sample
5
1 2 4
1 3 3
2 4 2
2 5 1
3
1 4
1 5
3 4
6

5 9

</p>