#include <stdio.h>
int A[100000];
// returns number of elements strictly smaller than "than"
// in array "A" from index "from" to "to" (both included)
int smaller(int than, int from, int to, int *A) {
if (from == to) return (A[from] < than);
int center = (from + to) / 2;
if (A[center] < than) {
// everything in A[from..center] is smaller
return (center - from + 1) + smaller(than, center + 1, to, A);
}
else {
// every solution is in A[from..center]
return smaller(than, from, center, A);
}
}
// returns number of elements strictly greater than "than"
// in array "A" from index "from" to "to" (both included)
int greater(int than, int from, int to, int *A) {
if (from == to) return (A[from] > than);
int center = (from + to + 1) / 2;
if (A[center] > than) {
// everything in A[center..to] is greater
return greater(than, from, center - 1, A) + (to - center + 1);
}
else {
// every solution is in A[center..to]
return greater(than, center, to, A);
}
}
int main() {
freopen("cautbin.in", "r", stdin);
freopen("cautbin.out", "w", stdout);
int n, m, i;
scanf("%i", &n);
for (i = 0; i < n; ++i) {
scanf("%i", &A[i]);
}
scanf("%i", &m);
for (i = 0; i < m; ++i) {
int x, y;
scanf("%i%i", &x, &y);
if (x == 0) {
int z = n - greater(y, 0, n - 1, A);
if (z == 0 || A[z - 1] != y) printf("-1\n");
else printf("%i\n", z);
}
else if (x == 1) {
printf("%i\n", n - greater(y, 0, n - 1, A));
}
else if (x == 2) {
printf("%i\n", 1 + smaller(y, 0, n - 1, A));
}
}
return 0;
}