import java.io.*;
import java.util.*;
class CSTREET
{
public static void main
(String args
[]) {
int t
=Integer.
parseInt(br.
readLine()); while(t--!=0)
{
ArrayList<Integer> V=new ArrayList<Integer>();
int p
=Integer.
parseInt(br.
readLine()); int n
=Integer.
parseInt(br.
readLine()); int m
=Integer.
parseInt(br.
readLine()); for(int i=0;i<m;i++)
{
String in
[]=br.
readLine().
split("\\s(?=\\S*$)"); int cost
=p
*Integer.
parseInt(in
[1]); if(!Map.
containsKey(cost
)) {
ArrayList<String> al=new ArrayList<String>();
al.add(in[0]);
}
else
{
ArrayList
<String
> get
=Map.
get(cost
); get.add(in[0]);
}
}
//iterate
long sum=0;
Map
<Integer,ArrayList
<String
>> map
= new TreeMap
<Integer,ArrayList
<String
>>(Map);//sorting by keys
while(it.hasNext())
{
int cost=pairs.getKey();
ArrayList<String> edges=pairs.getValue();// there can be multiple edges having single cost
Iterator<String> i=edges.iterator();
while(i.hasNext())
{
if(V.contains(u) && V.contains(v))// neglect this edge if part of same set V
{
continue;
}
else if( V.contains(u) && !V.contains(v) )
{
V.add(v);
sum+=cost;
}
else if( !V.contains(u) && V.contains(v) )
{
V.add(u);
sum+=cost;
}
else if( !V.contains(u) && !V.contains(v) )
{
V.add(u);
V.add(v);
sum+=cost;
}
}
}
}
}
}
CgoKCQkKCQkJaW1wb3J0IGphdmEuaW8uKjsKaW1wb3J0IGphdmEudXRpbC4qOwoJCQljbGFzcyBDU1RSRUVUCgkJCXsKCQkJCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZyBhcmdzW10pCgkJCQl0aHJvd3MgSU9FeGNlcHRpb24KCQkJCXsKCQkJCQlCdWZmZXJlZFJlYWRlciBicj1uZXcgQnVmZmVyZWRSZWFkZXIobmV3IElucHV0U3RyZWFtUmVhZGVyKFN5c3RlbS5pbikpOwoJCQkJCWludCB0PUludGVnZXIucGFyc2VJbnQoYnIucmVhZExpbmUoKSk7CgkJCQkJd2hpbGUodC0tIT0wKQoJCQkJCXsKCQ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