#include <stdio.h>
int main()
{
int n,order[7],m,index,no=0;
char temp[10],leafs[1<<7+1],result[1<<7+1];
while(scanf("%d",&n)==1)
{
no++;
if(n==0) break;
for(int i=0;i<n;i++)
{
scanf("%s",temp);
order[i]=temp[1]-'0';
}
scanf("%s",leafs);
scanf("%d",&m);
int i;
for(i=0;i<m;i++)
{
index=1;
scanf("%s",temp);
for(int j=0;j<n;j++)
{
if(temp[order[j]-1]=='1')
index=2*index+1;
else
index=2*index;
}
result[i]=leafs[index-(1<<n)];
}
result[i]='\0';
printf("S-Tree #%d:\n%s\n\n",no,result);
}
return 0;
}
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=653