BLINNET - Bytelandian Blingors Network


We have discovered the fastest communication medium Bytelandian scientists announced, and they called it blingors. The blingors are incomparably better than other media known before. Many companies in Byteland started to build blingors networks, so the information society in the kingdom of Bytes is fact! The priority is to build the core of the blingors network, joinig main cities in the country. Assume there is some number of cities that will be connected at the beginning. The cost of building blingors connection between two cities depends on many elements, but it has been successfully estimated. Your task is to design the blingors network connections between some cities in this way that between any pair of cities is a communication route. The cost of this network should be as small as possible.

Remarks

  • The name of the city is a string of at most 10 letters from a,...,z.
  • The cost of the connection between two cities is a positive integer.
  • The sum of all connections is not greater than 232-1.
  • The number of cities is not greater than 10 000.

Input


s [number of test cases <= 10] n [number of cities <= 10 000] NAME [city name] p [number of neigbouring cities to the city NAME] neigh cost [neigh - the unique number of city from the main list cost - the cost of building the blingors connection from NAME to neigh] [empty line between test cases]

Output

[separate lines] cost [the minimum cost of building the blingors network]

Example

Input:
2

4
gdansk
2
2 1
3 3
bydgoszcz
3
1 1
3 1
4 4
torun
3
1 3
2 1
4 1
warszawa
2
2 4
3 1

3
ixowo
2
2 1
3 3
iyekowo
2
1 1
3 7
zetowo
2
1 3 
2 7


Output:
3
4
Warning: large Input/Output data, be careful with certain languages

hide comments
getting started: 2015-03-22 10:41:48

if a path from a to b exists is it necessary that another path with same weight from b to a is also there in the given graph ?

Jumpy: 2015-02-09 13:16:06

AC in one go... :)

kelaseek: 2014-06-28 17:06:02

priority queues wont give you tle...

californiagurl: 2014-03-10 18:31:49

getting WA....any tricky test case?
hell lot of WAs just bcz i was being stupid :/ finally green....

Last edit: 2014-03-14 20:01:24
LeppyR64: 2011-12-22 01:49:49

The solution fits within 2^31-1.

Kashyap Krishnakumar: 2011-07-09 19:37:52

You can safely assume that the weight of the path from city a to city b is the same as the weight of the path from city b to city a :-)


Added by:kuszi
Date:2004-06-01
Time limit:4s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:PAL