import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class Main{
private int N, M;
private int[] vector;
public void print(){
for(Integer nr: vector)
System.out.print(nr + " ");
System.out.println();
}
private int maxIndex(int st, int end, int x){
if(st > end)
return -1;
int mid = st + (end-st)/2;
if(vector[mid] == x){
int pos = mid;
while(vector[pos] == x)
pos++;
return pos;
}
else if(x < vector[mid])
return maxIndex(st, mid-1, x);
return maxIndex(mid+1, end, x);
}
private int maxIndexMin(int st, int end, int x){
if(st == end)
return st;
int mid = st + (end-st)/2;
if(vector[mid] == x){
int pos = mid;
while(vector[pos] == x)
pos++;
return pos;
}
else if(x < vector[mid])
return maxIndexMin(st, mid-1, x);
return maxIndexMin(mid+1, end, x);
}
private int minIndex(int st, int end, int x){
if(st == end)
return st+1;
int mid = st + (end-st)/2;
if(vector[mid] == x){
int pos = mid;
while(vector[pos] == x)
pos--;
return pos+2;
}
else if(x < vector[mid])
return minIndex(st, mid-1, x);
return minIndex(mid+1, end, x);
}
public void read(){
try{
BufferedReader in = new BufferedReader(new FileReader("cautbin.in"));
PrintWriter out = new PrintWriter("cautbin.out");
String line = in.readLine();
N = Integer.valueOf(line);
line = in.readLine();
vector = new int[N];
String[] nums = line.split(" ");
for(int i=0; i<N; i++)
vector[i] = Integer.valueOf(nums[i]);
line = in.readLine();
M = Integer.valueOf(line);
for(int i=0; i<M; i++){
line = in.readLine();
nums = line.split(" ");
int act = Integer.valueOf(nums[0]);
int x = Integer.valueOf(nums[1]);
switch(act){
case 0:{
int pos = this.maxIndex(0, N-1, x);
out.println(pos);
break;
}
case 1:{
int pos = this.maxIndexMin(0, N-1, x);
out.println(pos);
break;
}
case 2:{
int pos = this.minIndex(0, N-1, x);
out.println(pos);
break;
}
}
}
in.close();
out.close();
}catch(IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
Main test = new Main();
test.read();
//test.print();
}
}