#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+1]; // --->> see +1 added
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)); // -->> crash , because i==cities, but adjacencylist doesnot have cities index.
}
}
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;
//---->> see need check valid or not p element
if (dist[p.second] != p.first )
continue;
//------------------------------------------------
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+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKQp7CgogICAgaW50IHRlc3RjYXNlczsKICAgIGNpbiA+PiB0ZXN0Y2FzZXM7CiAgICB3aGlsZSh0ZXN0Y2FzZXMtLT4wKQogICAgewogICAgICAgIGludCBjaXRpZXM7CiAgICAgICAgY2luID4+IGNpdGllczsKICAgICAgICBzdHJpbmcgYXJyW2NpdGllcysxXTsKICAgICAgICB2ZWN0b3I8cGFpcjxpbnQsaW50Pj4gYWRqYWNlbmN5bGlzdFtjaXRpZXMrMV07IC8vIC0tLT4+IHNlZSArMSBhZGRlZAogICAgICAgIHVub3JkZXJlZF9tYXA8c3RyaW5nLGludD4gbXltYXA7CiAgICAgICAgZm9yKGludCBpPTE7aTw9Y2l0aWVzO2krKykKICAgICAgICB7CiAgICAgICAgICAgIGNpbiA+PiBhcnJbaV07CiAgICAgICAgICAgIG15bWFwW2FycltpXV09aTsKICAgICAgICAgICAgaW50IHRlbXA7CiAgICAgICAgICAgIGNpbiA+PiB0ZW1wOwogICAgICAgICAgICB3aGlsZSh0ZW1wLS0+MCkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaW50IG5yLGNvc3Q7CiAgICAgICAgICAgICAgICBjaW4gPj4gbnIgPj4gY29zdDsKICAgICAgICAgICAgICAgIGFkamFjZW5jeWxpc3RbaV0ucHVzaF9iYWNrKHBhaXI8aW50LGludD4oY29zdCxucikpOyAgLy8gLS0+PiBjcmFzaCAsIGJlY2F1c2UgaT09Y2l0aWVzLCBidXQgYWRqYWNlbmN5bGlzdCBkb2Vzbm90IGhhdmUgY2l0aWVzIGluZGV4LgogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGludCB4eDsKICAgICAgICBjaW4gPj4geHg7CiAgICAgICAgd2hpbGUoeHgtLT4wKQogICAgICAgIHsKICAgICAgICAgICAgc3RyaW5nIHNvdXJjZSxkZXN0aW5hdGlvbjsKICAgICAgICBjaW4gPj4gIHNvdXJjZSA+PiBkZXN0aW5hdGlvbjsKICAgICAgICBpbnQgaW5kZXgxPW15bWFwW3NvdXJjZV0saW5kZXgyPW15bWFwW2Rlc3RpbmF0aW9uXTsKICAgICAgICBpbnQgZGlzdFtjaXRpZXMrMV07CiAgICAgICAgZm9yKGludCBpPTE7aTw9Y2l0aWVzO2krKykKICAgICAgICB7CiAgICAgICAgICAgIGRpc3RbaV09MTAwMDA7CiAgICAgICAgfQogICAgICAgIGRpc3RbaW5kZXgxXT0wOwogICAgICAgIHByaW9yaXR5X3F1ZXVlPHBhaXI8aW50LGludD4sdmVjdG9yPHBhaXI8aW50LGludD4+LGdyZWF0ZXI8cGFpcjxpbnQsaW50Pj4+IG15cXVldWU7CiAgICAgICAgbXlxdWV1ZS5wdXNoKHBhaXI8aW50LGludD4oZGlzdFtpbmRleDFdLGluZGV4MSkpOwogICAgICAgIHdoaWxlKG15cXVldWUuc2l6ZSgpKQogICAgICAgIHsKICAgICAgICAgICAgcGFpcjxpbnQsaW50PiBwPW15cXVldWUudG9wKCk7CiAgICAgICAgICAgIG15cXVldWUucG9wKCk7CiAgICAgICAgICAgIC8vY291dCA8PCAiZXh0cmFjdGVkIG1pbiAiIDw8IHAuc2Vjb25kIDw8ICIgYW5zIGRpc3RhbmNlIG9mIGl0IGlzICIgPDwgcC5maXJzdCA8PCBlbmRsOwogICAgICAgICAgICAKICAgICAgICAgICAgLy8tLS0tPj4gc2VlIG5lZWQgY2hlY2sgdmFsaWQgb3Igbm90IHAgZWxlbWVudAogICAgICAgICAgICBpZiAoZGlzdFtwLnNlY29uZF0gIT0gcC5maXJzdCApCiAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgLy8tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0KICAgICAgICAgICAgZm9yKHBhaXI8aW50LGludD4gdnYgOiBhZGphY2VuY3lsaXN0W3Auc2Vjb25kXSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaWYoZGlzdFt2di5zZWNvbmRdPmRpc3RbcC5zZWNvbmRdK3Z2LmZpcnN0KQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGRpc3RbdnYuc2Vjb25kXT1wLmZpcnN0K3Z2LmZpcnN0OwogICAgICAgICAgICAgICAgICAgLy8gY291dCA8PCAicHVzaGluZyAiIDw8IHZ2LnNlY29uZCA8PCAiIHZlcnRleCBhbnMgaXRzIGRpc3RhbmNlIGlzICIgPDwgZGlzdFt2di5zZWNvbmRdIDw8IGVuZGw7CiAgICAgICAgICAgICAgICAgICAgbXlxdWV1ZS5wdXNoKHBhaXI8aW50LGludD4oZGlzdFt2di5zZWNvbmRdLHZ2LnNlY29uZCkpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGNvdXQgPDwgZGlzdFtpbmRleDJdIDw8IGVuZGw7CiAgICAgICAgfQogICAgICAgIAogICAgfQp9Cg==
MQo0CmdkYW5zawoyCjIgMQozIDMKYnlkZ29zemN6CjMKMSAxCjMgMQo0IDQKdG9ydW4KMwoxIDMKMiAxCjQgMQp3YXJzemF3YQoyCjIgNAozIDEKMgpnZGFuc2sgd2Fyc3phd2EKYnlkZ29zemN6IHdhcnN6YXdhCg==
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