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

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

hide comments
2017-03-05 12:02:08
Ac in one go, classical MST.
2017-01-18 13:19:51
City names are completely unnecessary here. AC by kruskal's alg
2017-01-16 10:49:02 mateifl
Do we even need the city names for the solution?
2016-12-03 11:28:49
easy with mst!!
2016-08-15 16:05:49
my 50th :D
2015-09-16 00:42:19 sujit yadav
classical MST :p
2015-08-31 10:22:31 7Bubble
love u.. C++11. Just 55 lines of codes and acc in one go!
2015-07-28 21:36:19 (Tjandra Satria Gunawan)(曾毅昆)
constraints is not clear, range of p (or sum of p for all cities) should be mentioned.
2015-06-27 17:03:11 Ayon Das
Classical MST. Should be moved to tutorial!
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.