Under construction

Still being built. Things may change

The fastest road to Banikoara

Interview

Time limit2sMemory limit512 MB

Summary
Given towns joined by undirected weighted roads, find the shortest travel distance between a named departure town and destination town.
Level

Medium4 of 10

Topics
Graph, Shortest path, Hash map, Heap
Solved
No attempts yet

Problem

Codjo: I am ready for the trip to Banikoara.

Bossi: Let us go through Parakou.

Assiba: No, the fastest road to Banikoara goes through Djougou.

You are given a list of roads between towns and the length of each road. Write a program that reports the shortest distance to travel from a town A to a town B.

Roads are two way. A road line in the input can be travelled in either direction. Two towns may be joined by more than one road, and a road may have the same town at both ends.

Input

The first line contains one integer PP (1≤P≤10001 \le P \le 1000), the number of data sets that follow.

Each data set begins with a line containing the number NN of remaining lines in the data set (1≤N≤5001 \le N \le 500), a space, the name of the departure town, a space, and the name of the destination town. Each of the following NN lines contains the name of one town, a space, the name of another town, a space, and the distance in kilometers between those two towns.

Every distance is an integer between 11 and 10000001000000. A town name is a string of at most 3030 characters formed with [a-z], [A-Z] and the sign "-", and names are case sensitive. A route from the departure town to the destination town always exists.

Output

For each data set, print one line containing the name of the departure town, a space, the name of the destination town, a space, and the shortest distance to travel between those two towns.

If the departure town and the destination town are the same, the shortest distance is 00.

Examples7

  1. Example 1

    Input
    1
    4 Dassa-Zoume Banikoara
    Dassa-Zoume Djougou 270
    Banikoara Djougou 211
    Parakou Banikoara 284
    Parakou Dassa-Zoume 225
    
    Expected output
    Dassa-Zoume Banikoara 481
    
  2. Example 2

    Input
    1
    3 Parakou Parakou
    Parakou Djougou 100
    Djougou Banikoara 50
    Parakou Banikoara 400
    
    Expected output
    Parakou Parakou 0
    
  3. Example 3

    Input
    1
    1 Cotonou Porto-Novo
    Cotonou Porto-Novo 32
    
    Expected output
    Cotonou Porto-Novo 32
    
  4. Example 4

    Input
    1
    5 Kandi Malanville
    Kandi Malanville 90
    Kandi Malanville 75
    Malanville Kandi 61
    Kandi Nikki 10
    Nikki Malanville 12
    
    Expected output
    Kandi Malanville 22
    
  5. Example 5

    Input
    1
    5 Save Save-Ville
    Save Save 7
    Save Bohicon 5
    Bohicon Save-Ville 6
    Save Save-Ville 20
    Save-Ville Save-Ville 3
    
    Expected output
    Save Save-Ville 11
    
  6. Example 6

    Input
    1
    4 Ouidah Tanguieta
    Allada Ouidah 18
    Come Allada 25
    Tanguieta Come 40
    Ouidah Tanguieta 200
    
    Expected output
    Ouidah Tanguieta 83
    
  7. Example 7

    Input
    3
    2 Natitingou Kandi
    Natitingou Kandi 214
    Natitingou Kandi 199
    1 Lokossa Lokossa
    Cotonou Ouidah 40
    6 Abomey-Calavi Nikki
    Abomey-Calavi Bohicon 120
    Bohicon Parakou 180
    Parakou Nikki 100
    Abomey-Calavi Save 160
    Save Parakou 140
    Nikki Bohicon 320
    
    Expected output
    Natitingou Kandi 199
    Lokossa Lokossa 0
    Abomey-Calavi Nikki 400