Under construction

Still being built. Things may change

Countries at War

Interview

Time limit2sMemory limit512 MB

Summary
Given directed weighted edges between cities, treat mutually reachable cities as zero-cost (same country), then answer shortest path queries.
Level

Medium7 of 10

Topics
Shortest path, Graph, Union-find, DFS
Solved
No attempts yet

Problem

In 2050, after several attempts by an international body to keep the peace, the third world war breaks out. Industrial, commercial and military secrets force every country to run a highly sophisticated spy service, so every city in the world holds at least one spy from each country. A spy has to reach other spies, informants and their own headquarters while on an assignment. There is no safe channel in wartime, so every message goes out encoded and only its recipient reads what it says.

Spies use the one service that still runs during the war, the mail. Each city has one post office. A letter can go straight to the post office of the destination city, or it can pass through other post offices on the way.

The post office in city AA can send a printed letter directly to the post office in city BB when the two have a delivery agreement. The agreement fixes the time in hours that the letter needs to travel from AA to BB. An agreement in the opposite direction does not have to exist. Without an agreement between AA and BB, office AA relays the letter through as many other offices as it takes.

Some post offices are linked by electronic means such as satellites and optical fiber. Before the war those links reached every office and a letter arrived instantly. Once the war started each country took control of its own electronic communication, so an office can send a letter electronically, that is with no time at all, only to an office in the same country. Offices AA and BB are in the same country when a printed letter is delivered from AA to BB and also from BB to AA.

Your spy service holds every delivery agreement in the world. Find the minimum time to send a letter for several pairs of cities.

Input

The input holds several test cases.

The first line of each test case has two integers separated by a space, the number of cities NN (1≤N≤5001 \le N \le 500) and the number of delivery agreements EE (0≤E≤N20 \le E \le N^2). Cities are numbered 1 to NN.

The next EE lines each hold three integers separated by spaces, XX, YY and HH (1≤X,Y≤N1 \le X, Y \le N, 1≤H≤10001 \le H \le 1000). An agreement sends a printed letter from city XX to city YY, and that letter arrives HH hours later.

The line after that holds the number of queries KK (0≤K≤1000 \le K \le 100). The following KK lines each hold two integers separated by a space, OO and DD (1≤O,D≤N1 \le O, D \le N). For each query, determine the minimum time to send a letter from city OO to city DD.

A line where NN and EE are both 0 ends the input. That line is not a test case.

Output

For each test case print KK lines. Line II holds the minimum time in whole hours for query II. When there is no way to deliver a letter between the two cities of a query, print Nao e possivel entregar a carta on that line, exactly as written and without quotation marks.

After each test case print one blank line.

Examples2

  1. Example 1

    Input
    4 5
    1 2 5
    2 1 10
    3 4 8
    4 3 7
    2 3 6
    5
    1 2
    1 3
    1 4
    4 3
    4 1
    3 3
    1 2 10
    2 3 1
    3 2 1
    3
    1 3
    3 1
    3 2
    0 0
    
    Expected output
    0
    6
    6
    0
    Nao e possivel entregar a carta
    
    10
    Nao e possivel entregar a carta
    0
    
  2. Example 2

    Input
    3 2
    1 2 5
    2 3 7
    6
    1 1
    1 2
    1 3
    2 1
    3 1
    3 3
    0 0
    
    Expected output
    0
    5
    12
    Nao e possivel entregar a carta
    Nao e possivel entregar a carta
    0