#include
using namespace std;
struct node{
int data;
struct node *next;
};
struct node *p;
void addtrail(node &head,int data){
noden=new node;
n->data=data;
n->next=NULL;
if(head==NULL){
head=n;
p=n;
}
p->next=n;
p=n;
}
void merge(node *&head,int n,node *&head1,int m){
if(n>m){
while(n!=m){
head=head->next;
n=n-1;
}
}
if(n<m){
while(n!=m){
head1=head1->next;
m=m-1;
}
}
while(head&&head1){
if((head->data)==(head1->data)){
cout<data;
break;
}
head=head->next;
head1=head1->next;
}
if(head==NULL&&head1==NULL){
cout<<-1;
}
}
int main() {
int n;
cin>>n;
int a[n];
node *head=NULL;
for(int i=0;i<n;i++){
cin>>a[i];
addtrail(head,a[i]);
}
int m;
cin>>m;
int b[m];
node *head1=NULL;
for(int j=0;j<m;j++){
cin>>b[j];
addtrail(head1,b[j]);
}
merge(head,n,head1,m);
return 0;
}