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

#FW0000002. Thành phố ánh sáng chói lòa (Floyd: City of Blinding Lights)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Thành phố ánh sáng chói lòa (Floyd: City of Blinding Lights)

Floyd: City of Blinding Lights

Source: HackerRank

Version: Phuoc Hung OJ Extended

Problem Statement

A directed weighted graph is given. Answer the shortest-path distance for each ordered query; return −1-1 when unreachable.

Input

n,mn,m; mm directed edges; qq; qq queries.

Output

Print the answer to every query on its own line.

Subtasks

  • Subtask 1 (40 points): n≤8n\le 8, q≤12q\le 12; 1≤n≤4001\le n\le 400; 0≤m≤1000000\le m\le 100000; 1≤q≤1000001\le q\le 100000; 1≤u,v≤n1\le u,v\le n; 1≤w≤1000001\le w\le 100000.
  • Subtask 2 (60 points): 1≤n≤4001\le n\le 400; 0≤m≤1000000\le m\le 100000; 1≤q≤1000001\le q\le 100000; 1≤u,v≤n1\le u,v\le n; 1≤w≤1000001\le w\le 100000.

Examples

Input

3 3
1 2 5
2 3 2
1 3 10
3
1 3
3 1
2 2

Output

7
-1
0

Explanation

The route 1→2→31\to2\to3 costs 77 versus 1010 directly. No route goes from 33 back to 11; the distance from 22 to itself is zero.