#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int f[100006],x,k,i,m,j,ct,ok;
int main()
{
freopen("cautbin.in","r",stdin);
freopen("cautbin.out","w",stdout);
scanf("%d",&x);
for(k=1;k<=x;k++)
scanf("%d",&f[k]);
sort(f+1,f+x+1);
scanf("%d",&m);
for(k=1;k<=m;k++)
{
scanf("%d%d",&j,&i);
if(j==0)
{
ct=upper_bound(f+1,f+x+1,i)-f-1;
if(ct<=x&&ct>=1&&f[ct]==i)
printf("%d\n",ct);
else
printf("-1\n");
}
if(j==1)
{
ct=lower_bound(f+1,f+x+1,i+1)-f-1;
printf("%d\n",ct);
}
if(j==2)
{
ct=upper_bound(f+1,f+x+1,i-1)-f;
printf("%d\n",ct);
}
}
return 0;
}