/*
思路:先建立树,再遍历找两个节点,保存树根节点到要找的节点的序列,再比较序列相同的点
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std ;
typedef struct node
{
int data ;
struct node *left , *right ;
}node , *tree ;
void Init(tree &T){
T = (node *)malloc(sizeof(node));
T->left = T->right = NULL ;
return ;
}
void PreorderTree(tree &T)
{
int ch ;
cin >> ch ;
if(ch == 0 )
{
T=NULL;
return ;
}
T=(node*)malloc(sizeof(node)) ;
T->data = ch ;
PreorderTree(T->left);
PreorderTree(T->right);
return ;
}
int a[1002] , b[1002] , j = 0 , tag = false;
void PreorderTra(tree T , int node1 )
{
if(T){
if(T->data == node1){
a[j++] = T->data ;
tag = true ;
}
if(!tag){
a[j++] = T->data ;
PreorderTra(T->left , node1 ) ;
PreorderTra(T->right , node1 );
if(!tag)
j-- ;
}
}
}
void PreorderTra1(tree T , int node1 )
{
if(T){
if(T->data == node1){
b[j++] = T->data ;
tag = true ;
}
if(!tag)
{
b[j++] = T->data ;
PreorderTra1(T->left , node1 ) ;
PreorderTra1(T->right , node1 );
if(!tag)
j-- ;
}
}
}
int main(void)
{
int t ;
cin >> t ;
while(t --){
tree T;
Init(T);
PreorderTree(T);
int i , node1 , node2 ;
cin >> node1 >> node2 ;
/*if(node1 == node2){
cout<<node1<<endl;
continue ;
}*/
j = 0 ;
tag = false;
PreorderTra(T , node1 );
int ka = j ;
/* for( i = 0 ; i < ka ; i ++)
cout<<a[i]<<" ";
cout<<endl; */
j = 0 ;
tag = false ;
PreorderTra1(T,node2 );
int kb = j ;
/*for( i = 0 ; i < kb ; i ++)
cout<<b[i]<<" ";
cout<<endl; */
bool flag = false ;
for( i = ka-1 ; i >=0 ; i--){
for( j = kb-1; j >=0 ; j--)
if(a[i] == b[j]){
cout<<a[i]<<endl;
flag = true ;
break;
}
if(flag)
break;
}
if(!flag)
cout<<"My God"<<endl;
}
return 0;
}