#include <bits/stdc++.h>
using namespace std;
#define INF32_MAX 2147483647
#define endl "\n"
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = x * 10 + ch - 48;
ch = getchar();
}
return x * f;
}
const int N = 1e6;
struct node
{
int dist, val, idx;
node *ls, *rs, *fa;
explicit node()
{
dist = val = idx = 0;
ls = rs = fa = nullptr;
}
};
node *root[N];
bitset<N> st;
int n, m;
node *get_fa(node *u)
{
if (u == nullptr || u->fa == nullptr)
return u;
while (u->fa != nullptr && u != nullptr)
u = u->fa;
return u;
}
node *merge(node *L, node *R)
{
if (!L || !R)
return (L == nullptr ? R : L);
if (L->val < R->val || (L->val == R->val && L->idx > R->idx))
swap(L, R);
L->rs = merge(L->rs, R);
if (L->rs)
L->rs->fa = L;
if (!L->ls)
L->ls = L->rs, L->rs = nullptr;
else if (L->ls->dist < L->rs->dist)
swap(L->ls, L->rs);
L->dist = (L->rs ? L->rs->dist + 1 : 0);
return L;
}
node *pop(node *u)
{
if (!u)
return nullptr;
if (u->ls)
u->ls->fa = u->ls;
if (u->rs)
u->rs->fa = u->rs;
st[u->idx] = true;
auto res = merge(u->ls, u->rs);
u->ls = u->rs = nullptr;
return res;
}
signed main()
{
while (~scanf("%d", &n))
{
for (int i = 1; i <= n; i++)
{
root[i] = new node();
root[i]->val = read(), root[i]->idx = i;
}
m = read();
for (int i = 1; i <= m; i++)
{
int x, y;
x = read(), y = read();
auto fx = get_fa(root[x]);
auto fy = get_fa(root[y]);
if (fx == fy && fx && fy)
cout << -1 << endl;
else
{
auto ffx = pop(fx);
fx->val /= 2;
fx = merge(ffx, fx);
auto ffy = pop(fy);
fy->val /= 2;
fy = merge(ffy, fy);
cout << merge(fx, fy)->val << endl;
}
}
}
return 0;
}