#include <bits/stdc++.h>   using namespace std;   int arr[500005], brr[500005];   pair<int, int> tree[500005 * 4];   vector<int> vv[500005];   vector<pair<int, int> > qr[500005];   int ans[500005];   void build(int node, int b, int e) {     if (b == e) {       tree[node] = make_pair(500000000, 500000000);       return;     }     int left = 2 * node, right = left + 1, mid = (b + e) / 2;     build(left, b, mid);     build(right, mid + 1, e);     tree[node] = tree[left];   }   void update(int node, int b, int e, int i, int v) {     if (e < i || b > i) return;     if (b == e && b == i) {       tree[node] = make_pair(v, arr[b]);       return;     }     int left = 2 * node, right = left + 1, mid = (b + e) / 2;     update(left, b, mid, i, v);     update(right, mid + 1, e, i, v);     if (tree[left].first <= tree[right].first) {       tree[node] = tree[left];     } else       tree[node] = tree[right];   }   pair<int, int> query(int node, int b, int e, int l, int r) {     if (b > r || e < l) return make_pair(500000000, 500000000);     if (b >= l && e <= r) return tree[node];     int left = 2 * node, right = left + 1, mid = (b + e) / 2;     pair<int, int> p = query(left, b, mid, l, r);     pair<int, int> q = query(right, mid + 1, e, l, r);     if (p.first <= q.first) return p;     return q;   }   int main() {     int n;     scanf( %d , &n);     int mx = 0;     for (int i = 1; i <= n; i++) {       scanf( %d , &arr[i]);       vv[arr[i]].push_back(i);       mx = max(mx, arr[i]);     }     int q;     scanf( %d , &q);     for (int i = 1; i <= q; i++) {       int l, r;       scanf( %d %d , &l, &r);       qr[r].push_back(make_pair(l, i));     }     build(1, 1, n);     for (int i = 1; i <= n; i++) {       int id = lower_bound(vv[arr[i]].begin(), vv[arr[i]].end(), i) -                vv[arr[i]].begin();       int v;       if (id == 0) {         v = 1;         brr[arr[i]] = i;       } else {         v = vv[arr[i]][id - 1] + 1;         update(1, 1, n, brr[arr[i]], 500000000);         brr[arr[i]] = i;       }       update(1, 1, n, i, v);       for (int j = 0; j < qr[i].size(); j++) {         pair<int, int> ret = query(1, 1, n, qr[i][j].first, i);         if (qr[i][j].first >= ret.first) ans[qr[i][j].second] = ret.second;       }     }     for (int i = 1; i <= q; i++) printf( %d n , ans[i]);   }   