#include <bits/stdc++.h>
using namespace std;
int main()
{
int testcases;
cin >> testcases;
while(testcases-->0)
{
int cities;
cin >> cities;
string arr[cities+1];
vector<pair<int,int>> adjacencylist[cities];
unordered_map<string,int> mymap;
for(int i=1;i<=cities;i++)
{
cin >> arr[i];
mymap[arr[i]]=i;
int temp;
cin >> temp;
while(temp-->0)
{
int nr,cost;
cin >> nr >> cost;
adjacencylist[i].push_back(pair<int,int>(cost,nr));
}
}
int xx;
cin >> xx;
while(xx-->0)
{
string source,destination;
cin >> source >> destination;
int index1=mymap[source],index2=mymap[destination];
int dist[cities+1];
for(int i=1;i<=cities;i++)
{
dist[i]=10000;
}
dist[index1]=0;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> myqueue;
myqueue.push(pair<int,int>(dist[index1],index1));
while(myqueue.size())
{
pair<int,int> p=myqueue.top();
myqueue.pop();
//cout << "extracted min " << p.second << " ans distance of it is " << p.first << endl;
for(pair<int,int> vv : adjacencylist[p.second])
{
if(dist[vv.second]>dist[p.second]+vv.first)
{
dist[vv.second]=p.first+vv.first;
// cout << "pushing " << vv.second << " vertex ans its distance is " << dist[vv.second] << endl;
myqueue.push(pair<int,int>(dist[vv.second],vv.second));
}
}
}
cout << dist[index2] << endl;
}
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmludCBtYWluKCkKewogICAgaW50IHRlc3RjYXNlczsKICAgIGNpbiA+PiB0ZXN0Y2FzZXM7CiAgICB3aGlsZSh0ZXN0Y2FzZXMtLT4wKQogICAgewogICAgICAgIGludCBjaXRpZXM7CiAgICAgICAgY2luID4+IGNpdGllczsKICAgICAgICBzdHJpbmcgYXJyW2NpdGllcysxXTsKICAgICAgICB2ZWN0b3I8cGFpcjxpbnQsaW50Pj4gYWRqYWNlbmN5bGlzdFtjaXRpZXNdOwogICAgICAgIHVub3JkZXJlZF9tYXA8c3RyaW5nLGludD4gbXltYXA7CiAgICAgICAgZm9yKGludCBpPTE7aTw9Y2l0aWVzO2krKykKICAgICAgICB7CiAgICAgICAgICAgIGNpbiA+PiBhcnJbaV07CiAgICAgICAgICAgIG15bWFwW2FycltpXV09aTsKICAgICAgICAgICAgaW50IHRlbXA7CiAgICAgICAgICAgIGNpbiA+PiB0ZW1wOwogICAgICAgICAgICB3aGlsZSh0ZW1wLS0+MCkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaW50IG5yLGNvc3Q7CiAgICAgICAgICAgICAgICBjaW4gPj4gbnIgPj4gY29zdDsKICAgICAgICAgICAgICAgIGFkamFjZW5jeWxpc3RbaV0ucHVzaF9iYWNrKHBhaXI8aW50LGludD4oY29zdCxucikpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGludCB4eDsKICAgICAgICBjaW4gPj4geHg7CiAgICAgICAgd2hpbGUoeHgtLT4wKQogICAgICAgIHsKICAgICAgICAgICAgc3RyaW5nIHNvdXJjZSxkZXN0aW5hdGlvbjsKICAgICAgICBjaW4gPj4gIHNvdXJjZSA+PiBkZXN0aW5hdGlvbjsKICAgICAgICBpbnQgaW5kZXgxPW15bWFwW3NvdXJjZV0saW5kZXgyPW15bWFwW2Rlc3RpbmF0aW9uXTsKICAgICAgICBpbnQgZGlzdFtjaXRpZXMrMV07CiAgICAgICAgZm9yKGludCBpPTE7aTw9Y2l0aWVzO2krKykKICAgICAgICB7CiAgICAgICAgICAgIGRpc3RbaV09MTAwMDA7CiAgICAgICAgfQogICAgICAgIGRpc3RbaW5kZXgxXT0wOwogICAgICAgIHByaW9yaXR5X3F1ZXVlPHBhaXI8aW50LGludD4sdmVjdG9yPHBhaXI8aW50LGludD4+LGdyZWF0ZXI8cGFpcjxpbnQsaW50Pj4+IG15cXVldWU7CiAgICAgICAgbXlxdWV1ZS5wdXNoKHBhaXI8aW50LGludD4oZGlzdFtpbmRleDFdLGluZGV4MSkpOwogICAgICAgIHdoaWxlKG15cXVldWUuc2l6ZSgpKQogICAgICAgIHsKICAgICAgICAgICAgcGFpcjxpbnQsaW50PiBwPW15cXVldWUudG9wKCk7CiAgICAgICAgICAgIG15cXVldWUucG9wKCk7CiAgICAgICAgICAgIC8vY291dCA8PCAiZXh0cmFjdGVkIG1pbiAiIDw8IHAuc2Vjb25kIDw8ICIgYW5zIGRpc3RhbmNlIG9mIGl0IGlzICIgPDwgcC5maXJzdCA8PCBlbmRsOwogICAgICAgICAgICBmb3IocGFpcjxpbnQsaW50PiB2diA6IGFkamFjZW5jeWxpc3RbcC5zZWNvbmRdKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpZihkaXN0W3Z2LnNlY29uZF0+ZGlzdFtwLnNlY29uZF0rdnYuZmlyc3QpCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgZGlzdFt2di5zZWNvbmRdPXAuZmlyc3QrdnYuZmlyc3Q7CiAgICAgICAgICAgICAgICAgICAvLyBjb3V0IDw8ICJwdXNoaW5nICIgPDwgdnYuc2Vjb25kIDw8ICIgdmVydGV4IGFucyBpdHMgZGlzdGFuY2UgaXMgIiA8PCBkaXN0W3Z2LnNlY29uZF0gPDwgZW5kbDsKICAgICAgICAgICAgICAgICAgICBteXF1ZXVlLnB1c2gocGFpcjxpbnQsaW50PihkaXN0W3Z2LnNlY29uZF0sdnYuc2Vjb25kKSk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgY291dCA8PCBkaXN0W2luZGV4Ml0gPDwgZW5kbDsKICAgICAgICB9CiAgICAgICAgCiAgICB9Cn0K
MQo0CmdkYW5zawoyCjIgMQozIDMKYnlkZ29zemN6CjMKMSAxCjMgMQo0IDQKdG9ydW4KMwoxIDMKMiAxCjQgMQp3YXJzemF3YQoyCjIgNAozIDEKMgpnZGFuc2sgd2Fyc3phd2EKYnlkZ29zemN6IHdhcnN6YXdh
1
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
2
gdansk warszawa
bydgoszcz warszawa