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

#FW0000025. Công ty Scrooge (The Scrooge Co Problem)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Công ty Scrooge (The Scrooge Co Problem)

The Scrooge Co Problem

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

A delivery company has named locations and a directed travel-cost matrix, where −1-1 means no direct connection. For each employee, find a route from the requested origin to the destination, following directed edges only. The reimbursement is the minimum total cost of the roads used. Output the complete route, or an explicit impossibility message when the destination cannot be reached.

Input

nn, one tab-separated line of nn location names, an n×nn\times n matrix, qq, then qq rows employee start destination (single-token names in PHOJ).

Output

For employee name traveling from start to end, if reachable, print two lines in the exact format:

Mr name to go from start to end, you will receive C euros

Path: start ... end

Here CC is the minimum travel cost, and the path is the complete sequence of location names separated by spaces. Any optimal route is valid when several exist. If unreachable, print only:

Sorry Mr name you can not go from start to end

Process requests in the given order.

Subtasks

  • Subtask 1 (40 points): n≤7n\le 7; 1≤n≤991\le n\le 99; 1≤q≤991\le q\le 99; −1≤cij≤300-1\le c_ij\le 300.
  • Subtask 2 (60 points): 1≤n≤991\le n\le 99; 1≤q≤991\le q\le 99; −1≤cij≤300-1\le c_ij\le 300.

Examples

Input

2
A	B
0 3
-1 0
2
John A B
Anna B A

Output

Mr John to go from A to B, you will receive 3 euros
Path: A B
Sorry Mr Anna you can not go from B to A

Explanation

John can travel A to B for 33 euros. Anna cannot travel B to A because the reverse road is missing.