ICPC 2021 · Problem B · Dungeon Crawler

45th ICPC · Dhaka, Bangladesh

Statement

Time limit: 5 seconds

The helix key Image generated by DALL-E Alice and Bob are in charge of testing a new escape room! In this escape room, customers are trapped in a dungeon and have to explore the entire area. The dungeon consists of nn rooms connected by exactly n1n- 1 corridors. It is possible to travel between any pair of rooms using these corridors.

Two of the dungeon rooms are special. One of these rooms contains a pro- tective idol known as the “helix key.” A different room contains a nasty “dome trap,” which prevents the player from moving once activated. Enter- ing the room with the trap before acquiring the key will result in the player being trapped in the dungeon forever. The player cannot start in the same room as the key or the trap.

There are qq different scenarios that Alice and Bob wish to examine. In the iith scenario, the player starts in room sis_{i}, the key is in room kik_{i}, and the trap is in room tit_{i}. For each scenario, compute the minimum amount of time needed to explore the entire dungeon without getting trapped.

Input

The first line of input contains two integers nn and qq, where nn (3n20003 \le n \le 2 000) is the number of rooms and qq (1q2000001\le q \le 200 000) is the number of scenarios to consider. Rooms are numbered from 11 to nn. The next n1n- 1 lines each contain three integers uu, vv, and ww indicating that there is a corridor between rooms uu and vv (1u,vn,u=v1\le u, v \le n, u=v) that takes time ww (1w1091\le w \le 10^{9}) to traverse.

Then follow qq lines: the iith of these lines contains three distinct integers sis_{i}, kik_{i}, and tit_{i} (1si,ki,ti1 \le si, ki, ti \le nn) indicating the room where the player starts, the room with the key, and the room with the trap, respectively.

Output

For each scenario, output the minimum amount of time needed to visit every room at least once. If it is impossible to visit every room at least once, output impossible.

Sample Input 1

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

Sample Output 1

15
17
impossible
12

ICPC World Finals 2021 Problem B: Dungeon Crawler

Sample Input 2

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

Sample Output 2

11
impossible
10
10

ICPC World Finals 2021 Problem B: Dungeon Crawler

No official solution in the source collection.