#include <iostream>
#include <cstdio>
#include <map>
#include <string>
#include <cstring>
using namespace std;
const int maxc = 100;
const int maxt = 1000;
int c,t,head[maxc+1],link[maxc*maxt+1],adj1[maxc*maxt+1],adj2[maxc*maxt+1],adj3[maxc*maxt+1],tim,s,f,dp[maxc+1][2400+1];
string ss,ff;
map <string,int> m;
void enter()
{
memset(head,0,sizeof(head));
m.clear();
cin >> c;
for (int i=1; i<=c; i++)
{
string s1;
cin >> s1;
m[s1]=i;
}
cin >> t;
int socanh=0;
for (int i=1; i<=t; i++)
{
int ti;
cin >> ti;
//cout << ti << '\n';
int cs,pre;
for (int j=1; j<=ti; j++)
{
string s1,s2;
cin >> s1 >> s2;
while ((s1[0]=='0') and (s1.size()>1)) s1.erase(0,1);
if (j==1)
{
cs=m[s2];
pre=stoi(s1);
}
else
{
int cs2=m[s2];
int pre2=stoi(s1);
socanh++;
link[socanh]=head[cs2];
adj1[socanh]=cs;
adj2[socanh]=pre2;
adj3[socanh]=pre;
head[cs2]=socanh;
pre=pre2;
cs=cs2;
}
}
}
string s1,s2;
cin >> s1;
while ((s1[0]=='0') and (s1.size()>1)) s1.erase(0,1);
tim=stoi(s1);
cin >> s1 >> s2;
s=m[s1];
f=m[s2];
ss=s1;
ff=s2;
//cout << s << ' ' << f << '\n';
//for (int i=1; i<=c; i++)
// cout << head[i] << ' ' << adj1[head[i]] << '\n';
}
int caldp(int x, int y)
{
//cout << x << ' ' << y << '\n';
if (dp[x][y]==-2)
{
if (x==s) dp[x][y]=y;
else
{
dp[x][y]=-1;
int i=head[x];
//cout << i << '\n';
while (i!=0)
{
if (adj2[i]<=y) dp[x][y]=max(dp[x][y],caldp(adj1[i],adj3[i]));
i=link[i];
}
}
}
//cout << x << ' ' << y << ' ' << dp[x][y] << '\n';
return dp[x][y];
}
void solve()
{
for (int i=1; i<=c; i++)
for (int j=1; j<=2400; j++)
dp[i][j]=-2;
//cout << dp[f][1411] << '\n';
//cout << "I'm here\n";
int mt;
for (mt=0; mt<=2400; mt++)
if ((caldp(f,mt)>=0) and (caldp(f,mt)>=tim)) break;
if ((mt==2400) or (caldp(f,mt)<tim)) cout << "No connection\n";
else
{
string p=to_string(caldp(f,mt));
while (p.size()<4) p='0'+p;
cout << "Departure " << p << ' ' << ss << '\n';
p=to_string(mt);
while (p.size()<4) p='0'+p;
cout << "Arrival " << p << ' ' << ff << '\n';
}
}
int main()
{
ios_base::sync_with_stdio(0);
//freopen("10039.inp","r",stdin);
//freopen("10039.out","w",stdout);
int t;
cin >> t;
int i=0;
for (int i=1; i<=t; i++)
{
cout << "Scenario " << i << '\n';
enter();
solve();
cout << '\n';
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0ZGlvPgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8Y3N0cmluZz4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKY29uc3QgaW50IG1heGMgPSAxMDA7CmNvbnN0IGludCBtYXh0ID0gMTAwMDsKaW50IGMsdCxoZWFkW21heGMrMV0sbGlua1ttYXhjKm1heHQrMV0sYWRqMVttYXhjKm1heHQrMV0sYWRqMlttYXhjKm1heHQrMV0sYWRqM1ttYXhjKm1heHQrMV0sdGltLHMsZixkcFttYXhjKzFdWzI0MDArMV07CnN0cmluZyBzcyxmZjsKbWFwIDxzdHJpbmcsaW50PiBtOwp2b2lkIGVudGVyKCkKewoJbWVtc2V0KGhlYWQsMCxzaXplb2YoaGVhZCkpOwoJbS5jbGVhcigpOwoJY2luID4+IGM7Cglmb3IgKGludCBpPTE7IGk8PWM7IGkrKykKCXsKCQlzdHJpbmcgczE7CgkJY2luID4+IHMxOwoJCW1bczFdPWk7Cgl9CgljaW4gPj4gdDsKCWludCBzb2Nhbmg9MDsKCWZvciAoaW50IGk9MTsgaTw9dDsgaSsrKQoJewoJCWludCB0aTsKCQljaW4gPj4gdGk7CgkJLy9jb3V0IDw8IHRpIDw8ICdcbic7CgkJa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