HACKERS - Hackers

no tags 

The network of your office is composed of several computers and bidirectional links. Each link connects a given pair of computers and has an access value. Each user in the network has an access privilege, and is able to use any link with access value not exceeding his access privilege.

Everything was fine until you notice that a bunch of hackers are accessing the network. You know that if there is a link between computers A and B, with access value V, and a hacker with access privilege of at least V controls A, then he can control B. Hackers wish to control the most important computers by exploiting problems in the network. They are trying to increase their access privileges in order to use the links, and your task is to measure how safe the network is.

Given the description of the network, the computer each hacker currently controls and the target computer each hacker wishes to control, you need to calculate the minimum access privilege each hacker needs to get in order to control his target computer. Hackers act independently, neither they collaborate nor interfere with each other. This means that each hacker may control each computer and use each link independently of what the other hackers do.

Input

Each test case is described using several lines. The first line contains three integers C, L and H, indicating the number of computers, links and hackers in the network, respectively (2 ≤ C ≤ 3000, 1 ≤ L, H ≤ 105).

Each computer is identified by an integer number between 1 and C. Each of the next L lines describes a different bidirectional link using three integers A, B and V; the numbers A and B identify two distinct computers that are the endpoints of the link (1 ≤ A < B ≤ C); the number V is the access value of the link, that is, any hacker must have an access privilege of at least V to use the link (1 ≤ V ≤ 109).

Each of the last H lines describes a different hacker using two distinct integers S and T that identify the computer that the hacker currently controls and the computer that the hacker wishes to control, respectively (1 ≤ S, T ≤ C).

You may assume that within each test case no two links connect the same pair of computers, and that for any pair of computers there is at least one sequence of links that allow to reach one computer starting from the other.

The end of input is indicated with a line containing the number −1 three times.

Output

For each test case, output a single line with H integers representing the minimum access privilege each hacker needs to achieve his goal. The result for each hacker must appear in the same order that the hackers are described in the input.

Example

Input:
5 6 4
1 2 4
1 3 5
2 4 3
2 5 1
3 4 2
4 5 2
3 2
2 4
1 5
3 1
2 1 1
1 2 1
2 1
2 1 3
1 2 1000000000
2 1
2 1
1 2
-1 -1 -1

Output:
2 2 4 4
1
1000000000 1000000000 1000000000

hide comments
Buda IM (retired): 2011-11-14 21:31:03

@yuzeming : I have same complexity as you and AC 4.4s with C++

yuzeming: 2010-10-21 03:00:34

Try O(C^2+H)
And it Will STILL TLE
I Try Many many many times :(

Last edit: 2011-04-18 08:32:34
Pablo Ariel Heiber: 2012-02-27 11:38:31

You should do better than H^2, L^2, LH, LC and HC (for instance, C^2 + H + L log L is fine).

ymGXX: 2010-09-30 09:24:34

interesting problem!

Spooky: 2010-09-28 12:11:10

great problem!


Added by:Pablo Ariel Heiber
Date:2010-09-27
Time limit:5.157s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 NODEJS OBJC VB.NET
Resource:FCEyN UBA ICPC Selection 2010