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

#FW0000003. Rủi ro (Risk)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Rủi ro (Risk)

Risk

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

There are exactly 2020 countries, numbered 11 through 2020. Each bidirectional border crossing costs one move. Find minimum moves for the queried pairs.

Input

For each country i=1,…,19i=1,\ldots,19, read cic_i followed by its cic_i neighbors; then qq and qq pairs.

Output

Print each result as u to v: d, with country IDs right-aligned to width two.

Subtasks

  • Subtask 1 (40 points): q≤10q\le 10; n=20n=20; 0≤ci≤190\le c_i\le 19; 1≤u,v≤201\le u,v\le 20; 1≤q≤501\le q\le 50.
  • Subtask 2 (60 points): n=20n=20; 0≤ci≤190\le c_i\le 19; 1≤u,v≤201\le u,v\le 20; 1≤q≤501\le q\le 50.

Examples

Input

1 2
2 1 3
2 2 4
2 3 5
2 4 6
2 5 7
2 6 8
2 7 9
2 8 10
2 9 11
2 10 12
2 11 13
2 12 14
2 13 15
2 14 16
2 15 17
2 16 18
2 17 19
2 18 20
2
1 20
3 3

Output

 1 to 20: 19
 3 to  3: 0

Explanation

The countries form the chain 1−2−⋯−201-2-\cdots-20, so the distance from 11 to 2020 is 1919 and from 33 to itself is 00.