#include <stdio.h>
#include <stdlib.h>
int OrderID0(int* vector, int N, int Value);
int OrderID1(int* vector, int N, int Value);
int OrderID2(int* vector, int N, int Value);
int BinarySearch(int* vector, int Bottom, int Top, int Value);
int main()
{
int N,M;
int i,Value;
int* vector;
int command;
FILE* input;
FILE* output;
output = fopen("cautbin.out","w");
input = fopen("cautbin.in","r");
fscanf(input, "%d", &N);
vector = (int*)malloc(N*sizeof(int));
for(i=0 ; i<N; i++)
fscanf(input, "%d", vector+i);
fscanf(input, "%d", &M);
for(i=0; i<M; i++)
{
fscanf(input, "%d", &command);
switch (command)
{
case 0:
fscanf(input,"%d",&Value);
fprintf(output,"%d\n",OrderID0(vector,N,Value)+1);
break;
case 1:
fscanf(input,"%d",&Value);
fprintf(output,"%d\n",OrderID1(vector,N,Value)+1);
break;
case 2:
fscanf(input,"%d",&Value);
fprintf(output,"%d\n",OrderID2(vector,N,Value)+1);
break;
}
}
fclose(output);
fclose(input);
}
int OrderID0(int* vector, int N, int Value)
{
int Index;
Index = BinarySearch(vector,0,N-1,Value);
if(Index == -1) return -2;
while(vector[Index+1] == Value && Index != (N-1)) Index++;
return Index;
}
int OrderID1(int* vector, int N, int Value)
{
int Index;
Index = BinarySearch(vector,0,N-1,Value);
while(vector[Index+1] <= Value && Index != (N-1)) Index++;
return Index;
}
int OrderID2(int* vector, int N, int Value)
{
int Index;
Index = BinarySearch(vector,0,N-1,Value);
while(vector[Index-1] >= Value && Index != (0)) Index--;
return Index;
}
int BinarySearch(int* vector, int Bottom, int Top, int Value)
{
if((Top - Bottom) < 2) return -1;
if(vector[(Top+Bottom)/2] > Value) return BinarySearch(vector,Bottom,(Top+Bottom)/2,Value);
if(vector[(Top+Bottom)/2] < Value) return BinarySearch(vector,(Top+Bottom)/2,Top,Value);
if(vector[(Top+Bottom)/2] == Value) return (Top+Bottom)/2;
}