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

#BF0000012. Giao thông thành phố (Traffic)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Giao thông thành phố (Traffic)

Traffic

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

A directed road u to v costs the cube of the difference in busyness. Return ? for unreliable distance queries.

Each city ii has a busyness value bib_i. A directed road from uu to vv has cost w(u,v)=(bv−bu)3w(u,v)=(b_v-b_u)^3, the cube of the difference in busyness. This value may be negative even though busyness values are nonnegative. Trips start in city 11.

For each query destination xx, find the shortest path cost from city 11. The answer is unreliable if xx is unreachable, if its shortest distance is less than 33, or if a negative cycle reachable from the source can affect xx. Print ? for all such cases and the integer distance otherwise.

Input

Read nn and nn busyness values, then rr directed road pairs u,vu,v, then qq and qq query vertices.

Output

Print Set #1, then an integer or ? for each query.

Subtasks

  • Subtask 1 (20 points): 2≤n≤202\le n\le20; 0≤m≤500\le m\le50; 1≤q≤1001\le q\le100; 1≤bi≤201\le b_i\le20; 1≤u,v,x≤n1\le u,v,x\le n.
  • Subtask 2 (30 points): 2≤n≤1502\le n\le150; 0≤m≤5000\le m\le500; 1≤q≤1001\le q\le100; 1≤bi≤201\le b_i\le20; 1≤u,v,x≤n1\le u,v,x\le n.
  • Subtask 3 (50 points): 2≤n≤2002\le n\le200; 0≤m≤40000\le m\le4000; 1≤q≤1001\le q\le100; 1≤bi≤201\le b_i\le20; 1≤u,v,x≤n1\le u,v,x\le n.

Examples

Input

5
6 7 8 9 10
6
1 2
2 3
3 4
1 5
5 4
4 5
2
4
5

Output

Set #1
3
4

Explanation

Relaxation finds eligible paths, or a negative cycle in the required scope. The sample uses the exact output tokens specified by the problem.