A signal starts at one computer in a network and spreads through communication channels to every other computer it can reach, directly or through other computers. Each channel is one-way and takes a known amount of time to carry the signal across.
Every computer forwards the signal onward the moment it receives it, so a computer receives the signal along whichever route reaches it soonest. The network is done once the last computer has received it.
Write a program that finds how long it takes for the signal to reach every computer in the network.
Input
First line of input will be a single integer - the number of testcases.
The first line of each testcase contains two integers and - the number of computers and the number of channels.
Each of the next lines contains three integers , and - a channel leading from computer to computer , which the signal needs time to cross. It does not carry the signal from to .
The last line of the testcase contains a single integer - the computer the signal starts from.
Computers are numbered to . There may be several channels between the same pair of computers, and a channel may lead from a computer back to itself.
Output
For every testcase print a single line with the time the signal needs to reach every computer, or if some computer cannot receive it at all.
Example
3 5 7 1 2 7 1 3 3 1 5 6 2 1 2 3 5 2 4 2 3 5 4 1 1 3 1 1 2 5 1 1 1 1 1 4 1
7 -1 0
In the first testcase the signal reaches computer in , and computer in - going beats the direct channel, which costs . From there computer is reached in , and computer in by the direct channel, since the route through would cost . The last arrival is at time .
In the second testcase nothing leads to computer , so the answer is . In the third the signal starts at the only computer and is already there, so it takes no time at all.
Constraints
and
The sum of over all testcases does not exceed , and the sum of does not exceed
This problem was adapted, with permission, from Kašnjenje signala, authored by Društvo matematičara Srbije and Fondacija Petlja.