#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
int pre[N];
unordered_map<int,bool> mp;
int main(){
int n,m;scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
scanf("%d",&pre[i]);
mp[pre[i]]=true;
}
while(n--){
int x,y;scanf("%d%d",&x,&y);
if(!mp[x]&&!mp[y]) printf("ERROR: %d and %d are not found.\n",x,y);
else if(!mp[x]) printf("ERROR: %d is not found.\n",x);
else if(!mp[y]) printf("ERROR: %d is not found.\n",y);
else{
int i;
for(i=0;i<m;i++){
if((pre[i]>=x&&pre[i]<=y)||(pre[i]<=x&&pre[i]>=y)) break;
}
if(pre[i]!=x&&pre[i]!=y){
printf("LCA of %d and %d is %d.\n",x,y,pre[i]);
}
else if(pre[i]==x){
printf("%d is an ancestor of %d.\n",x,y);
}
else if(pre[i]==y){
printf("%d is an ancestor of %d.\n",y,x);
}
}
}
return 0;
}