#include<iostream>
#include<stdio.h>
using namespace std;
struct BiNode {
char data;
BiNode *l,*r;
BiNode() {
l=r=NULL;
}
};
class Tree {
public:
BiNode* p;
BiNode* temp;
Tree() {
root=Creat();
p=NULL;
temp=NULL;
}
void PreOder() {
if(root) {
PreOder(root);
cout<<endl;
}
}
BiNode* parent(char x) {
parent(root,x,p);
return temp;
}
~Tree() {
Release(root);
}
private:
BiNode *root;
void parent(BiNode* root,char x,BiNode* p) {
if(root) {
if(root->data==x) {
temp=p;
return;
}
parent(root->l,x,root);
parent(root->r,x,root);
}
}
BiNode* Creat() {
BiNode *bt;
char ch;
cin>>ch;
if(ch=='#') {
bt=NULL;
} else {
bt=new BiNode;
bt->data=ch;
bt->l=Creat();
bt->r=Creat();
}
return bt;
}
void Release(BiNode *bt) {
if(bt) {
Release(bt->l);
Release(bt->r);
delete bt;
}
}
void PreOder(BiNode *bt) {
if(bt) {
cout<<bt->data;
PreOder(bt->l);
PreOder(bt->r);
}
}
};
int main() {
int n;
while(cin>>n) {
while(n--) {
Tree t;
char x;
cin>>x;
BiNode* ret=t.parent(x);
if(!ret) {
cout<<"null"<<endl;
} else {
cout<<ret->data<<endl;
}
}
}
return 0;
}