// Given two linked list find and print the intersection of these two.
#include<iostream>
using namespace std;
class node
{
public:
	int data;
	node* next; 
	node(int data){
		this->data = data;
		next = NULL;
	}
	
};
void InsertAtTail(node *&head, int data){
	if(head==NULL){
		head = new node(data);
		return;
	}
	node *temp = head;
	while(temp->next!=NULL){
		temp = temp->next;
	}
	temp->next = new node(data);
	return;
}
node* Intersection(node *a, node *b){
	node *temp = NULL;
	while(a!=NULL && b!=NULL){
		if(a->data==b->data){
			InsertAtTail(temp,a->data);
			a = a->next;
			b = b->next;
		}
		else if(a->data<b->data){
			a = a->next;
		}
		else{
			b = b->next;
		}
	}
	return temp;
}
node* IntersectionRecursive(node *a, node *b, node *&temp){
	if(a==NULL || b==NULL){
		return temp;
	}
	if(a->data==b->data){
		InsertAtTail(temp,a->data);
		return IntersectionRecursive(a->next,b->next,temp);
	}
	else if(a->data<b->data){
		return IntersectionRecursive(a->next,b,temp);
	}
	else{
		return IntersectionRecursive(a,b->next,temp);
	}
}
// Following commented function is not working! The dummy concept is not working here!.
// node* SortedIntersection(node *a, node *b){
// 	node dummy;
// 	node *temp = &dummy;
// 	while(a!=NULL && b!=NULL){
// 		if(a->data==b->data){
// 			InsertAtTail(temp->next,a->data);
// 			temp = temp->next;
// 			a = a->next;
// 			b = b->next;
// 		}
// 		else if(a->data<b->data){
// 			a = a->next;
// 		}
// 		else{
// 			b = b->next;
// 		}
// 	}
// 	return temp->next;
// }
void printLL(node *head){
	while(head!=NULL){
		cout<<head->data;
		if(head->next!=NULL){
			cout<<"->";
		}
		head = head->next;
	}
	cout<<endl;
}
int main()
{
	node *first = NULL;
	node *second = NULL;
	int n,data;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>data;
		InsertAtTail(first,data);
	}
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>data;
		InsertAtTail(second,data);
	}
	node *temp = NULL;
	temp = Intersection(first,second);
	// temp = IntersectionRecursive(first,second,temp);
	// temp = SortedIntersection(a,b,temp);
	printLL(temp);
	return 0;
}