#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define MAXN 10005
int pre[MAXN];
int in[MAXN];
struct G{
int value;
G *left=NULL,*right=NULL;
};
G* build(G *root,int prel,int prer,int inl,int inr){
int i, left, right;
for (i = inl; i <= inr; i++)
{
if (in[i] == pre[prel]) break;
}
if (!root){
root = new G;
root->value=in[i];
root->left=root->right=NULL;
}
if (i != inr)
{
root->right = build(root->right, prel+1+i-1-inl+1, prer, i+1, inr);
}
if (i != inl)
{
root->left = build(root->left, prel+1, prel+1+i-1-inl, inl, i-1);
}
return root;
}
bool findG(G *head,int t){
if(!head){
return false;
}else{
if(t<head->value){
return findG(head->left,t);
}else if(t==head->value){
return true;
}else{
return findG(head->right,t);
}
}
}
int LCA(G *head,int a,int b){
if(a>b){
int t=a;
a=b;
b=t;
}
if(a<=head->value&&head->value<=b){
return head->value;
}else if(a<head->value&&b<head->value){
return LCA(head->left,a,b);
}else{
return LCA(head->right,a,b);
}
}
int main()
{
int M,N,t;
G *head=NULL;
scanf("%d %d",&M,&N);
for(int i=0;i<N;i++){
scanf("%d",&t);
pre[i]=t;
in[i]=t;
}
sort(pre,pre+N);
head=build(head,0,N-1,0,N-1);
while(M--){
int a,b;
scanf("%d %d",&a,&b);
int as,bs;
as=findG(head,a);
bs=findG(head,b);
if(!as&&!bs){
printf("ERROR: %d and %d are not found.\n",a,b);
}else if(!as){
printf("ERROR: %d is not found.\n",a);
}else if(!bs){
printf("ERROR: %d is not found.\n",b);
}else{
int ans=LCA(head,a,b);
if(ans==a){
printf("%d is an ancestor of %d.\n",a,b);
}else if(ans==b){
printf("%d is an ancestor of %d.\n",b,a);
}else{
printf("LCA of %d and %d is %d.\n",a,b,ans);
}
}
}
return 0;
}