PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000098. Phí qua đường (Toll)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Phí qua đường (Toll)

Toll

Source: Baltic Olympiad in Informatics

Version: Phuoc Hung OJ Extended

Problem Statement

A delivery company wants to transport goods between locations. Every road is one-way and has a positive toll. For each order, find the minimum sum of tolls along a directed route from the departure location to the destination, or print -1 if no route exists.

Locations are numbered from 00 to n−1n-1. An integer kk divides the locations into consecutive layers, where the layer of location vv is ⌊v/k⌋\lfloor v/k\rfloor. Every directed edge a→ba\to b goes to exactly the next layer, satisfying

$$\left\lfloor\frac{b}{k}\right\rfloor=\left\lfloor\frac{a}{k}\right\rfloor+1.$$

Find the minimum toll for each order.

Input

The first line contains four integers k,n,m,ok,n,m,o. The next mm lines each contain a,b,ta,b,t, a directed edge a→ba\to b with toll tt. The final oo lines each contain a,ba,b, an order from location aa to bb.

Output

For every order, print its minimum toll on a separate line, or -1 if no directed path exists.

Subtasks

  • Subtask 1 (20 points): 1≤k≤51\le k\le5; 1≤n≤500001\le n\le50000; 1≤o≤100001\le o\le10000; 0≤a<b<n0\le a<b<n trong mỗi đơn hàng; 1≤t≤100001\le t\le10000; mọi cạnh đi đúng một tầng về phía trước, không có hai cạnh cùng cặp đầu mút. mm không vượt quá số cạnh hợp lệ có thể có giữa các tầng. Additionally, n≤10n\le10, o≤10o\le10.
  • Subtask 2 (30 points): 1≤k≤51\le k\le5; 1≤n≤500001\le n\le50000; 1≤o≤100001\le o\le10000; 0≤a<b<n0\le a<b<n trong mỗi đơn hàng; 1≤t≤100001\le t\le10000; mọi cạnh đi đúng một tầng về phía trước, không có hai cạnh cùng cặp đầu mút. mm không vượt quá số cạnh hợp lệ có thể có giữa các tầng. Additionally, n≤2000n\le2000, o≤2000o\le2000.
  • Subtask 3 (50 points): 1≤k≤51\le k\le5; 1≤n≤500001\le n\le50000; 1≤o≤100001\le o\le10000; 0≤a<b<n0\le a<b<n trong mỗi đơn hàng; 1≤t≤100001\le t\le10000; mọi cạnh đi đúng một tầng về phía trước, không có hai cạnh cùng cặp đầu mút. mm không vượt quá số cạnh hợp lệ có thể có giữa các tầng.

Examples

Input

5 14 5 5
0 5 9
5 12 10
0 7 7
7 12 8
4 7 10
0 12
0 5
0 7
7 12
0 13

Output

15
9
7
8
-1

Explanation

The cheapest route from 00 to 1212 is 0→7→120\to7\to12 with total toll 7+8=157+8=15, rather than 0→5→120\to5\to12 costing 1919. The three single-edge routes cost 9,7,89,7,8. Location 1313 is unreachable, so the final answer is -1.