#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;
}