n=gets.to_i
bd=0
hotel=[]
hotel << 0 #dummy
gets.split.each do |x|
  t=x.to_i
  hotel << t
  bd |= 1 << t
end
#p bd.to_s 2
L=gets.to_i
l=(1 << L+2)-1

cnt=0
#p L
#p (l << L -1).to_s(2)
#puts
(gets.to_i).times do
  a,b=gets.split.map &:to_i
  if a>b then a^=b;b^=a;a^=b end
  path = ( bd  & ( ( 1 << hotel[b] +1) -1) ) >> hotel[a] 
#  p path.to_s 2

  while path > 1 do
    
    path >>= Math.log2(path & l).floor
#    p path.to_s 2
 
#    p Math.log2(path & l)
#    path >>= Math.log2(path & l).floor
    #print path.to_s(2)," ", Math.log2(path & l).ceil,"\n"
  #  path=0
    cnt+=1
  end   
#  end
  p cnt
  cnt=0
end
