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
robbin_ha: 2017-03-05 12:02:08

Ac in one go, classical MST.

flyingduchman_: 2017-01-18 13:19:51

City names are completely unnecessary here. AC by kruskal's alg

mateifl: 2017-01-16 10:49:02

Do we even need the city names for the solution?

vanvinhbk94: 2016-12-03 11:28:49

easy with mst!!

kushalanand: 2016-08-15 16:05:49

my 50th :D

sujit yadav: 2015-09-16 00:42:19

classical MST :p

7Bubble: 2015-08-31 10:22:31

love u.. C++11. Just 55 lines of codes and acc in one go!

(Tjandra Satria Gunawan)(曾毅昆): 2015-07-28 21:36:19

constraints is not clear, range of p (or sum of p for all cities) should be mentioned.

Ayon Das: 2015-06-27 17:03:11

Classical MST. Should be moved to tutorial!


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