Beyond the Rescue
Time limit3sMemory limit256 MB
Find the fastest path from s to t in a weighted tree that avoids every street the guards walk on their cyclic route, printed modulo 998244353 or -1.
- Level
Hard9 of 10
- Topics
- Tree, Shortest path, Intervals
- Solved
- No attempts yet
Statement
Riatla is a little-known member of the Assassins Order (well-known assassins, as you can guess, do not live long). After performing one mission, Riatla faces a problem: the streets of the city are flooded by guards, and he needs a new escape plan from the crime scene.
The city has a tree-like structure. There are squares, connected with two-way streets, and there is exactly one path between every two squares.
The guards patrol the city in a cyclic route. They choose a sequence of squares . After visiting square , the guards head to square . If the two squares are not directly connected, the guards take the shortest path between them. After visiting , they go to and start the route again.
Riatla is good at disguising. Squares are crowded, so he can stay on a square without being noticed, even if the guards are on the same square. Streets are nearly empty, though, so if Riatla and the guards are on the same street at any moment, the guards shoot him with bows, regardless of the distance between them.
Each street takes time for the guards and time for Riatla to cross. Compute the minimal time Riatla needs to get from square , where he is now, to square , where his friends are waiting. Print the answer modulo .
Input
The first line contains one integer , the number of test cases. Each test case starts with two integers and , the number of squares and the number of important squares on the guards' path ().
Each of the next lines contains four integers , , , : the two squares joined by a street, the time the guards need to walk it, and the time Riatla needs to walk it (; ). The given graph is guaranteed to be a tree.
The next line contains integers , the important squares on the guards' path in visiting order (; ; ).
The last line contains two integers and , the starting square and the target square (; ).
The sum of and the sum of over all test cases do not exceed each.
Output
For each test case, print on its own line the minimal time Riatla needs, modulo . If Riatla cannot reach without being spotted, print .