This page is still under construction.

Parts of this page are still being built. What you see may change.

Regular checkup

Time limit1.5sMemory limit256 MB

Summary
In a graph split by a river with B bridges, answer Q queries for the shortest travel time from a given house to a given hospital, or -1 if unreachable.
Level

Medium7 of 10

Topics
Graph, Shortest path, Dynamic programming, Matrix
Solved
No attempts yet

Problem

The residents of DD Planet travel to a hospital for a regular checkup.

DD Planet has NN houses (numbered 1,2,…,N1, 2, \dots, N) and MM hospitals (numbered N+1,N+2,…,N+MN+1, N+2, \dots, N+M). A deep river separates the area holding the houses from the area holding the hospitals. Crossing the river is possible only over a bridge. There are BB bridges (numbered N+M+1,N+M+2,…,N+M+BN+M+1, N+M+2, \dots, N+M+B), and crossing any bridge takes 00 seconds.

DD Planet also has KK roads that join houses, hospitals, and bridges. A road between two bridges is allowed. Because the deep river runs between the two areas, no road joins a house directly to a hospital.

Answer QQ questions.

  • What is the minimum time for a resident of house SiS_i to reach hospital EiE_i?

Input

The first line contains the number of houses NN and the number of hospitals MM (1≤N,M≤10 0001 \le N, M \le 10\,000), the number of bridges BB (1≤B≤1001 \le B \le 100), the number of roads KK (1≤K≤2×1041 \le K \le 2 \times 10^4), and the number of questions QQ (1≤Q≤1051 \le Q \le 10^5), separated by spaces.

Each of the next KK lines contains three integers aa, bb, and KiK_i, separated by spaces. A road joins point aa and point bb, and travelling along that road takes KiK_i (1≤Ki≤1091 \le K_i \le 10^9) time.

Each of the next QQ lines contains SiS_i and EiE_i for one question, separated by spaces. (1≤Si≤N1 \le S_i \le N, N+1≤Ei≤N+MN+1 \le E_i \le N+M)

Output

On line ii, print the answer to question ii. If house SiS_i cannot reach hospital EiE_i, print −1-1.

Examples4

  1. Example 1

    Input
    2 2 1 5 2
    1 5 3
    2 5 1
    3 5 3
    4 5 3
    1 2 1
    1 3
    2 4
    
    Expected output
    5
    4
    
  2. Example 2

    Input
    2 2 2 3 3
    1 5 2
    3 5 5
    2 6 1
    1 3
    1 4
    2 3
    
    Expected output
    7
    -1
    -1
    
  3. Example 3

    Input
    1 1 2 4 1
    1 3 10
    1 4 1
    2 3 1
    2 4 100
    1 2
    
    Expected output
    11
    
  4. Example 4

    Input
    1 1 2 3 1
    1 3 5
    3 4 2
    4 2 7
    1 2
    
    Expected output
    14