#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <vector>
using namespace std;
struct Schedule{
int to;
int start,end;
Schedule(int a,int b,int c)
{
to = a; start = b; end = c;
}
};
vector<Schedule> vb[101];
int kase=1;
map<string,int> mp;
char dest_city[1024], start_city[1024];
int earliest_start_time, cityNum;
int dist[2400][101];//saves maximum departure time from start
void solve()
{
int st_city = mp[start_city];
int end_city = mp[dest_city];
int reach_time,reach_city;
memset(dist,-1,sizeof(dist));
printf("Scenario %d\n",kase++);
for(int i=0;i<vb[st_city].size();i++)
{
if(vb[st_city][i].start>=earliest_start_time)
{
reach_time = vb[st_city][i].end;
reach_city = vb[st_city][i].to;
dist[reach_time][reach_city]= max( dist[reach_time][reach_city] , vb[st_city][i].start );
}
}
for(int i=earliest_start_time;i<2400;i++)
{
for(int j=0;j<cityNum;j++)
{
if(dist[i][j]==-1)
continue;
for(int k=0;k<vb[j].size();k++)
{
if(vb[j][k].start>=i)
{
dist[vb[j][k].end][vb[j][k].to] = max(dist[vb[j][k].end][vb[j][k].to],dist[i][j]);
}
}
}
if(dist[i][end_city]!=-1)
{
printf("Departure %04d %s\n",dist[i][end_city],start_city);
printf("Arrival %04d %s\n",i,dest_city);
return;
}
}
printf("No connection\n");
}
int main()
{
int prev_time, prev_city, cur_time, cur_city;
int trainNum, noOfCities, t, arrival_time;
char str[1024];
scanf("%d",&t);
while(t--)
{
scanf("%d",&cityNum);
mp.clear();
for(int i=0;i<cityNum;i++)
{
scanf("%s",str);
mp[str]=i;
vb[i].clear();
}
scanf("%d",&trainNum);
for(int i=1;i<=trainNum;i++)
{
scanf("%d",&noOfCities);
for(int j=0;j<noOfCities;j++)
{
scanf("%d %s",&arrival_time,str);
if(j>0 && arrival_time>prev_time)
{
cur_time = arrival_time;
cur_city = mp[str];
vb[prev_city].push_back(Schedule(cur_city,prev_time,cur_time));
}
prev_time = arrival_time;
prev_city = mp[str];
}
}
scanf("%d %s %s",&earliest_start_time,start_city,dest_city);
solve();
printf("\n");
}
return 0;
}