#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int n,t,Case=1,num;
while ( scanf ("%d",&n)&&n)
{
int k=0;
num= 2<<(n-1) ;
char leaf[260]={0};//叶子结点
char a[3];
char query[12]={0};//查询的序列
char res[1000]={0};//结果
int b[12]={0};//对应层数的Xi
for (int i=1;i<=n;i++)
{
scanf ("%s",a);
b[i]=a[1]-'0';
}
scanf ("%s",leaf);
scanf ("%d",&t);
for (int i=0;i<t;i++)
{
scanf ("%s",query);
int x=1;
for (int j=1;j<=n;j++)
{
if (query[b[j]-1]-'0'==0)
x=x*2;
else if (query[b[j]-1]-'0'==1)
x=x*2+1;
}
res[k]=leaf[x-num];
k++;
memset(query,0,sizeof(query));
}
res[k]=0;
printf("S-Tree #%d:\n",Case); Case++;
printf ("%s\n\n",res);
}
return 0;
}
/*
4
x4 x2 x3 x1
0101010101010101
5
0101
1010
1111
0000
0001
*/