#include <bits/stdc++.h>
//#define FIN "cautbin.in"
//#define FOUT "cautbin.out"
using namespace std;
//returns the largest index such that arr[i] == key
// or -1 whether the key is not in the array
int binary_search0(int *arr, int lo, int hi, int key) {
if(lo > hi) {
return -1;
}
int middle = (lo+hi)>>1;
if(arr[middle] == key) {
if(middle < hi) {
if(arr[middle+1] == key) {
return binary_search0(arr, middle+1, hi, key);
} else {
return middle;
}
} else {
return middle;
}
} else if(arr[middle] < key) {
return binary_search0(arr, middle+1, hi, key);
} else {
return binary_search0(arr, lo, middle-1, key);
}
}
//returns the largest index such as arr[i]<=key; such an index it will always exist.
int binary_search1(int *arr, int lo, int hi, int key) {
if(lo > hi) {
return -1;
}
int middle = (lo + hi) / 2;
if(arr[middle] <= key) {
if(middle < hi) {
if(arr[middle+1] > key) {
return middle;
} else {
return binary_search1(arr, middle + 1, hi, key);
}
} else {
return middle;
}
} else {
return binary_search1(arr, lo, middle-1, key);
}
}
//returns the smallest index such as arr[i]>=key; such an index it will always exist.
int binary_search2(int *arr, int lo, int hi, int key) {
if(lo > hi) {
return -1;
}
int middle = (lo + hi) / 2;
if(arr[middle] >= key) {
if(lo < middle) {
if(arr[middle-1] < key) {
return middle;
} else {
return binary_search2(arr, lo, middle-1, key);
}
} else {
return middle;
}
} else {
return binary_search2(arr, middle+1, hi, key);
}
}
int main(int argc, char const *argv[]) {
//ifstream fin(FIN);
//ofstream fout(FOUT);
typedef int (*fnPointer)(int*, int, int, int);
int length;
cin>>length;
int array[length];
for(int i = 0; i < length; ++i) cin>>array[i];
fnPointer fn[ 3 ] = {binary_search0,binary_search1,binary_search2};
int numOps;
cin>>numOps;
while(numOps--) {
int op, value;
cin>>op>>value;
int index = fn[op](array, 0, length-1, value);
if(index == -1) {
cout<<index<<"\n";
} else {
cout<<index+1<<"\n";
}
}
//fin.close();
//fout.close();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Ci8vI2RlZmluZSBGSU4gImNhdXRiaW4uaW4iCi8vI2RlZmluZSBGT1VUICJjYXV0YmluLm91dCIKCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgovL3JldHVybnMgdGhlIGxhcmdlc3QgaW5kZXggc3VjaCB0aGF0IGFycltpXSA9PSBrZXkKLy8gb3IgLTEgd2hldGhlciB0aGUga2V5IGlzIG5vdCBpbiB0aGUgYXJyYXkKaW50IGJpbmFyeV9zZWFyY2gwKGludCAqYXJyLCBpbnQgbG8sIGludCBoaSwgaW50IGtleSkgewoKICAgIGlmKGxvID4gaGkpIHsKICAgICAgcmV0dXJuIC0xOwogICAgfQoKICAgIGludCBtaWRkbGUgPSAobG8raGkpPj4xOwoKICAgIGlmKGFyclttaWRkbGVdID09IGtleSkgewoKICAgICAgICAgIGlmKG1pZGRsZSA8IGhpKSB7CgogICAgICAgICAgICAgICBpZihhcnJbbWlkZGxlKzFdID09IGtleSkgewoKICAgICAgICAgICAgICAgICAgcmV0dXJuIGJpbmFyeV9zZWFyY2gwKGFyciwgbWlkZGxlKzEsIGhpLCBrZXkpOwoKICAgICAgICAgICAgICAgfSBlbHNlIHsKCiAgICAgICAgICAgICAgICAgcmV0dXJuIG1pZGRsZTsKICAgICAgICAgICAgICAgfQogICAgICAgICAgfSBlbHNlIHsKCiAgICAgICAgICAgIHJldHVybiBtaWRkbGU7CiAgICAgICAgICB9CiAgICB9IGVsc2UgaWYoYXJyW21pZGRsZV0gPCBrZXkpIHsKCiAgICAgICAgICByZXR1cm4gYmluYXJ5X3NlYXJjaDAoYXJyLCBtaWRkbGUrMSwgaGksIGtleSk7CgogICAgfSBlbHNlIHsKCiAgICAgICAgICByZXR1cm4gYmluYXJ5X3NlYXJjaDAoYXJyLCBsbywgbWlkZGxlLTEsIGtleSk7CiAgICB9Cn0KCi8vcmV0dXJucyB0aGUgbGFyZ2VzdCBpbmRleCBzdWNoIGFzIGFycltpXTw9a2V5OyBzdWNoIGFuIGluZGV4IGl0IHdpbGwgYWx3YXlzIGV4aXN0LgppbnQgYmluYXJ5X3NlYXJjaDEoaW50ICphcnIsIGludCBsbywgaW50IGhpLCBpbnQga2V5KSB7CgoKICAgICAgaWYobG8gPiBoaSkgewoKICAgICAgICByZXR1cm4gLTE7CiAgICAgIH0KCiAgICAgIGludCBtaWRkbGUgPSAobG8gKyBoaSkgLyAyOwoKICAgICAgaWYoYXJyW21pZGRsZV0gPD0ga2V5KSB7CgogICAgICAgICAgICAgICBpZihtaWRkbGUgPCBoaSkgewoKICAgICAgICAgICAgICAgICAgICAgaWYoYXJyW21pZGRsZSsxXSA+IGtleSkgewoKICAgICAgICAgICAgICAgICAgICAgICByZXR1cm4gbWlkZGxlOwoKICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKCiAgICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIGJpbmFyeV9zZWFyY2gxKGFyciwgbWlkZGxlICsgMSwgaGksIGtleSk7CiAgICAgICAgICAgICAgICAgICAgIH0KCiAgICAgICAgICAgICAgIH0gZWxzZSB7CgogICAgICAgICAgICAgICAgIHJldHVybiBtaWRkbGU7CiAgICAgICAgICAgICAgIH0KCiAgICAgIH0gZWxzZSB7CgogICAgICAgIHJldHVybiBiaW5hcnlfc2VhcmNoMShhcnIsIGxvLCBtaWRkbGUtMSwga2V5KTsKICAgICAgfQoKfQoKLy9yZXR1cm5zIHRoZSBzbWFsbGVzdCBpbmRleCBzdWNoIGFzIGFycltpXT49a2V5OyBzdWNoIGFuIGluZGV4IGl0IHdpbGwgYWx3YXlzIGV4aXN0LgppbnQgYmluYXJ5X3NlYXJjaDIoaW50ICphcnIsIGludCBsbywgaW50IGhpLCBpbnQga2V5KSB7CgogICAgaWYobG8gPiBoaSkgewoKICAgICAgcmV0dXJuIC0xOwogICAgfQoKICAgIGludCBtaWRkbGUgPSAobG8gKyBoaSkgLyAyOwoKICAgIGlmKGFyclttaWRkbGVdID49IGtleSkgewoKICAgICAgICAgICAgIGlmKGxvIDwgbWlkZGxlKSB7CgogICAgICAgICAgICAgICAgICAgaWYoYXJyW21pZGRsZS0xXSA8IGtleSkgewoKICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIG1pZGRsZTsKCiAgICAgICAgICAgICAgICAgICB9IGVsc2UgewoKICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIGJpbmFyeV9zZWFyY2gyKGFyciwgbG8sIG1pZGRsZS0xLCBrZXkpOwogICAgICAgICAgICAgICAgICAgfQoKICAgICAgICAgICAgIH0gZWxzZSB7CgogICAgICAgICAgICAgICByZXR1cm4gbWlkZGxlOwogICAgICAgICAgICAgfQoKICAgIH0gZWxzZSB7CgogICAgICByZXR1cm4gYmluYXJ5X3NlYXJjaDIoYXJyLCBtaWRkbGUrMSwgaGksIGtleSk7CiAgICB9Cn0KCmludCBtYWluKGludCBhcmdjLCBjaGFyIGNvbnN0ICphcmd2W10pIHsKCiAgLy9pZnN0cmVhbSBmaW4oRklOKTsKICAvL29mc3RyZWFtIGZvdXQoRk9VVCk7CgogIHR5cGVkZWYgaW50ICgqZm5Qb2ludGVyKShpbnQqLCBpbnQsIGludCwgaW50KTsKCiAgaW50IGxlbmd0aDsKICBjaW4+Pmxlbmd0aDsKICBpbnQgYXJyYXlbbGVuZ3RoXTsKCiAgZm9yKGludCBpID0gMDsgaSA8IGxlbmd0aDsgKytpKSBjaW4+PmFycmF5W2ldOwoKICBmblBvaW50ZXIgZm5bIDMgXSA9IHtiaW5hcnlfc2VhcmNoMCxiaW5hcnlfc2VhcmNoMSxiaW5hcnlfc2VhcmNoMn07CgogIGludCBudW1PcHM7CgogIGNpbj4+bnVtT3BzOwoKICB3aGlsZShudW1PcHMtLSkgewoKICAgIGludCBvcCwgdmFsdWU7CgogICAgY2luPj5vcD4+dmFsdWU7CgogICAgaW50IGluZGV4ID0gZm5bb3BdKGFycmF5LCAwLCBsZW5ndGgtMSwgdmFsdWUpOwoKICAgIGlmKGluZGV4ID09IC0xKSB7CgogICAgICBjb3V0PDxpbmRleDw8IlxuIjsKCiAgICB9IGVsc2UgewoKICAgICAgY291dDw8aW5kZXgrMTw8IlxuIjsKICAgIH0KICB9CgogICAvL2Zpbi5jbG9zZSgpOwogICAvL2ZvdXQuY2xvc2UoKTsKCiAgIHJldHVybiAwOwogfQo=