Regular checkup

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.

Medium7GraphShortest pathDynamic programmingMatrixNo attempts yetTime limit1.5sMemory limit256 MB

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 (1N,M100001 \le N, M \le 10\,000), the number of bridges BB (1B1001 \le B \le 100), the number of roads KK (1K2×1041 \le K \le 2 \times 10^4), and the number of questions QQ (1Q1051 \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 (1Ki1091 \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. (1SiN1 \le S_i \le N, N+1EiN+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.