// NA
#include <bits/stdc++.h>
#define BUFFER 5
#define BUFF(x) x + BUFFER
#define N_MAX (int) (5e5)
#define NODES_MAX (int) (4 * N_MAX * log2(N_MAX))
#define mp make_pair
#define INF (int) 1e9
#pragma GCC optimize "O4"
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef unordered_map<int, int> umii;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
int n;
int in[BUFF(N_MAX)];
pii best;
struct N {
  int l, r;
  pii val;
  N() {}
  N(int l, int r, pii val) : l(l), r(r), val(val) {}
} nodes[BUFF(NODES_MAX)];
int nodes_ptr;
int roots[BUFF(N_MAX)];
int getMid(int l, int r) { return l + (r - l) / 2; }
int buildTree(int prev, int val, int idx, int l, int r) {
  if (l <= idx && idx <= r) {
    int cur = ++nodes_ptr;
    if (l == r)
      nodes[cur] = N(0, 0, { val, idx });
    else {
      int mid = getMid(l, r);
      int a = buildTree(nodes[prev].l, val, idx, l, mid);
      int b = buildTree(nodes[prev].r, val, idx, mid + 1, r);
      pii left = (a) ? nodes[a].val : mp(INF, 0);
      pii right = (b) ? nodes[b].val : mp(INF, 0);
      nodes[cur] = N(a, b, min(left, right));
    }
    return cur;
  }
  return prev;
}
void queryAns(int cur, int sl, int sr, int l, int r) {
  if (!cur || sr < l || r < sl || best <= nodes[cur].val)
    return;
  if (sl <= l && r <= sr)
    best = min(best, nodes[cur].val);
  else {
    int mid = getMid(l, r);
    queryAns(nodes[cur].l, sl, sr, l, mid);
    queryAns(nodes[cur].r, sl, r, mid + 1, r);
  }
}
void program() {
  cin >> n;
  umii mp;
  for (int i = 1, last_seen; i <= n; ++i) {
    roots[i] = roots[i - 1];
    cin >> in[i];
    last_seen = mp[in[i]] ? mp[in[i]] : -1;
    if (last_seen != -1)
      roots[i] = buildTree(roots[i], INF, last_seen, 1, n);
    roots[i] = buildTree(roots[i], last_seen, i, 1, n);
    mp[in[i]] = i;
  }
  int m;
  cin >> m;
  for (int i = 1, l, r; i <= m; ++i) {
    cin >> l >> r;
    best = { INF, 0 };
    queryAns(roots[r], l, r, 1, n);
    if (best.first < l)
      cout << in[best.second] << endl;
    else cout << "0" << endl;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  program();
  return 0;
}


// TLE on #9
#include <bits/stdc++.h>
#define BUFFER 5
#define BUFF(x) x + BUFFER
#define N_MAX (int) (5e5)
#define NODES_MAX (int) (4 * N_MAX * log2(N_MAX))
#define mp make_pair
#define INF (int) 1e9
#pragma GCC optimize "O4"
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef map<int, int> mii;
typedef unordered_map <int, int> umii;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
int n, t_time;
int in[BUFF(N_MAX)];
pii best;
struct N {
  int l, r, accessed_at;
  pii val;
  N() {}
  N(int l, int r, pii val, int a_time) : l(l), r(r), val(val), accessed_at(a_time) {}
} nodes[BUFF(NODES_MAX)];
int nodes_ptr;
int roots[BUFF(N_MAX)];
int getMid(int l, int r) { return l + (r - l) / 2; }
int buildTree(int prev, int val, int idx, int l, int r) {
  if (l <= idx && idx <= r) {
    int cur = (nodes[prev].accessed_at == t_time) ? prev : ++nodes_ptr;
    if (l == r)
      nodes[cur] = N(0, 0, { val, idx }, t_time);
    else {
      int mid = getMid(l, r);
      int a = buildTree(nodes[prev].l, val, idx, l, mid);
      int b = buildTree(nodes[prev].r, val, idx, mid + 1, r);
      pii left = (a) ? nodes[a].val : mp(INF, 0);
      pii right = (b) ? nodes[b].val : mp(INF, 0);
      nodes[cur] = N(a, b, min(left, right), t_time);
    }
    return cur;
  }
  return prev;
}
void queryAns(int cur, int sl, int sr, int l, int r) {
  if (!cur || sr < l || r < sl || best < nodes[cur].val)
    return;
  if (sl <= l && r <= sr)
    best = min(best, nodes[cur].val);
  else {
    int mid = getMid(l, r);
    queryAns(nodes[cur].l, sl, sr, l, mid);
    queryAns(nodes[cur].r, sl, r, mid + 1, r);
  }
}
void program() {
  cin >> n;
  umii mp;
  for (int i = 1, last_seen; i <= n; ++i, ++t_time) {
    roots[i] = roots[i - 1];
    cin >> in[i];
    last_seen = mp[in[i]] ? mp[in[i]] : -1;
    if (last_seen != -1)
      roots[i] = buildTree(roots[i], INF, last_seen, 1, n);
    roots[i] = buildTree(roots[i], last_seen, i, 1, n);
    mp[in[i]] = i;
  }
  int m;
  cin >> m;
  for (int i = 1, l, r; i <= m; ++i) {
    cin >> l >> r;
    best = { INF, 0 };
    queryAns(roots[r], l, r, 1, n);
    if (best.first < l)
      cout << in[best.second] << endl;
    else cout << "0" << endl;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  program();
  return 0;
}


// TLE on #9
#include <bits/stdc++.h>
#define BUFFER 5
#define BUFF(x) x + BUFFER
#define N_MAX (int) (5e5)
#define NODES_MAX (int) (4 * N_MAX * log2(N_MAX))
#define mp make_pair
#define INF (int) 1e9
#pragma GCC optimize "O4"
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef map<int, int> mii;
typedef unordered_map <int, int> umii;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
int n, t_time;
int in[BUFF(N_MAX)];
struct N {
  int l, r, accessed_at;
  pii val;
  N() {}
  N(int l, int r, pii val, int a_time) : l(l), r(r), val(val), accessed_at(a_time) {}
} nodes[BUFF(NODES_MAX)];
int nodes_ptr;
int roots[BUFF(N_MAX)];
int getMid(int l, int r) { return l + (r - l) / 2; }
int buildTree(int prev, int val, int idx, int l, int r) {
  if (l <= idx && idx <= r) {
    int cur = (nodes[prev].accessed_at == t_time) ? prev : ++nodes_ptr;
    if (l == r)
      nodes[cur] = N(0, 0, { val, idx }, t_time);
    else {
      int mid = getMid(l, r);
      int a = buildTree(nodes[prev].l, val, idx, l, mid);
      int b = buildTree(nodes[prev].r, val, idx, mid + 1, r);
      pii left = (a) ? nodes[a].val : mp(INF, 0);
      pii right = (b) ? nodes[b].val : mp(INF, 0);
      nodes[cur] = N(a, b, min(left, right), t_time);
    }
    return cur;
  }
  return prev;
}
void queryAns(int cur, int sl, int sr, int l, int r, pii &best) {
  if (!cur || sr < l || r < sl || best <= nodes[cur].val)
    return;
  if (sl <= l && r <= sr)
    best = min(best, nodes[cur].val);
  else {
    int mid = getMid(l, r);
    queryAns(nodes[cur].l, sl, sr, l, mid, best);
    queryAns(nodes[cur].r, sl, r, mid + 1, r, best);
  }
}
void program() {
  cin >> n;
  umii mp;
  for (int i = 1, last_seen; i <= n; ++i, ++t_time) {
    roots[i] = roots[i - 1];
    cin >> in[i];
    last_seen = mp[in[i]] ? mp[in[i]] : -1;
    if (last_seen != -1)
      roots[i] = buildTree(roots[i], INF, last_seen, 1, n);
    roots[i] = buildTree(roots[i], last_seen, i, 1, n);
    mp[in[i]] = i;
  }
  int m;
  cin >> m;
  pii ret;
  for (int i = 1, l, r; i <= m; ++i) {
    cin >> l >> r;
    ret = { INF, 0 };
    queryAns(roots[r], l, r, 1, n, ret);
    if (ret.first < l)
      cout << in[ret.second] << endl;
    else cout << "0" << endl;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  program();
  return 0;
}


// NA
#include <bits/stdc++.h>
#define BUFFER 5
#define BUFF(x) x + BUFFER
#define N_MAX (int) 5e5
#define VAL_MAX (int) 5e5
#define NODES_MAX (int) (8 * N_MAX * log2(N_MAX))
#define INF (N_MAX + 1)
// #pragma GCC optimize "O4"
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int, int> pii;
int n;
struct {
  int val, prev; // val -> raw value provided in input, prev -> value it represents in the tree
} positions[BUFF(N_MAX)];
int t_prev[BUFF(VAL_MAX)];
struct N {
  N() {}
  N(int l, int r, int min_v, int min_idx) : l(l), r(r), min_v(min_v), min_idx(min_idx) {}
  int l, r, min_v, min_idx;
} nodes[BUFF(NODES_MAX)];
int nodes_ptr;
int roots[BUFF(N_MAX)];
int getMid(int l, int r) { return l + (r - l) / 2; }
int buildTree(int prev, int pos, int val, int l, int r) {
  if (l <= pos && pos <= r) {
    int cur = ++nodes_ptr;
    if (l == r) {
      nodes[cur].min_v = val;
      nodes[cur].min_idx = pos;
    } else {
      int mid = getMid(l, r);
      int a = buildTree(nodes[prev].l, pos, val, l, mid);
      int b = buildTree(nodes[prev].r, pos, val, mid + 1, r);
      int a_val = (a) ? nodes[a].min_v : INF;
      int b_val = (b) ? nodes[b].min_v : INF;
      int min_val, min_idx;
      if (a_val < b_val)
        min_val = a_val, min_idx = nodes[a].min_idx;
      else min_val = b_val, min_idx = nodes[b].min_idx;
      nodes[cur] = N(a, b, min_val, min_idx);
    }
    return cur;
  }
  return prev;
}
int queryLowestIndex(int cur, int t_l, int t_r, int l, int r) {
  if (!cur || t_r < l || r < t_l)
    return INF;
  else if (t_l <= l && r <= t_r)
    return nodes[cur].min_idx;
  else {
    int mid = getMid(l, r);
    int a = queryLowestIndex(nodes[cur].l, t_l, t_r, l, mid);
    int b = queryLowestIndex(nodes[cur].r, t_l, t_r, mid + 1, r);
    return (positions[a].prev < positions[b].prev) ? a : b;
  }
}
int getOneOccurrenceNumber(int l, int r) {
  int lowest = queryLowestIndex(roots[r], l, r, 1, n);
  if (positions[lowest].prev == lowest || positions[lowest].prev < l)
    return true;
  return false;
}
void program() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &positions[i].val);
    roots[i] = roots[i - 1];
    if (t_prev[positions[i].val])
      roots[i] = buildTree(roots[i], t_prev[positions[i].val], INF, 1, n);
    positions[i].prev = (t_prev[positions[i].val]) ? t_prev[positions[i].val] : i;
    roots[i] = buildTree(roots[i], i, positions[i].prev, 1, n);
    t_prev[positions[i].val] = i;
  }
  int q;
  scanf("%d", &q);
  for (int i = 1, l, r; i <= q; ++i) {
    scanf("%d %d", &l, &r);
    printf("%d\n", getOneOccurrenceNumber(l, r));
  }
}
void ready() { positions[INF].prev = INF; }
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ready();
  program();
  return 0;
}


// NA
#include <bits/stdc++.h>
#define BUFFER 5
#define BUFF(x) x + BUFFER
#define N_MAX (int) (5e5)
#define NODES_MAX (int) (4 * N_MAX * log2(N_MAX))
#define mp make_pair
#define INF (int) 1e9
// #pragma GCC optimize "O4"
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef unordered_map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
int n;
int in[BUFF(N_MAX)];
struct N {
  int l, r;
  pii val;
  N() {}
  N(int l, int r, pii val) : l(l), r(r), val(val) {}
} nodes[BUFF(NODES_MAX)];
int nodes_ptr;
int roots[BUFF(N_MAX)];
int getMid(int l, int r) { return l + (r - l) / 2; }
int buildTree(int prev, int val, int idx, int l, int r) {
  if (l <= idx && idx <= r) {
    int cur = ++nodes_ptr;
    if (l == r)
      nodes[cur] = N(0, 0, { val, idx });
    else {
      int mid = getMid(l, r);
      int a = buildTree(nodes[prev].l, val, idx, l, mid);
      int b = buildTree(nodes[prev].r, val, idx, mid + 1, r);
      pii left = (a) ? nodes[a].val : mp(INF, 0);
      pii right = (b) ? nodes[b].val : mp(INF, 0);
      nodes[cur] = N(a, b, min(left, right));
    }
    return cur;
  }
  return prev;
}
pii queryAns(int cur, int sl, int sr, int l, int r) {
  if (!cur || sr < l || r < sl)
    return { INF, 0 };
  if (sl <= l && r <= sr)
    return nodes[cur].val;
  else {
    int mid = getMid(l, r);
    pii x = queryAns(nodes[cur].l, sl, sr, l, mid);
    pii y = queryAns(nodes[cur].r, sl, r, mid + 1, r);
    return min(x, y);
  }
}
void program() {
  cin >> n;
  mii mp;
  for (int i = 1, last_seen; i <= n; ++i) {
    roots[i] = roots[i - 1];
    cin >> in[i];
    last_seen = mp[in[i]] ? mp[in[i]] : -1;
    if (last_seen != -1)
      roots[i] = buildTree(roots[i], INF, last_seen, 1, n);
    roots[i] = buildTree(roots[i], last_seen, i, 1, n);
    mp[in[i]] = i;
  }
  int m;
  cin >> m;
  pii ret;
  for (int i = 1, l, r; i <= m; ++i) {
    cin >> l >> r;
    ret = queryAns(roots[r], l, r, 1, n);
    if (ret.first < l)
      cout << in[ret.second] << endl;
    else cout << "0" << endl;
  }
}
int main() {
  ios::sync_with_stdio(0);
	cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  program();
  return 0;
}


// NA
#include <bits/stdc++.h>
#define BUFFER 5
#define BUFF(x) x + BUFFER
#define N_MAX (int) (5e5)
#define NODES_MAX (int) (4 * N_MAX * log2(N_MAX))
#define mp make_pair
#define INF (int) 1e9
// #pragma GCC optimize "O4"
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
int n;
int in[BUFF(N_MAX)];
struct N {
  int l, r;
  pii val;
  N() {}
  N(int l, int r, pii val) : l(l), r(r), val(val) {}
} nodes[BUFF(NODES_MAX)];
int nodes_ptr;
int roots[BUFF(N_MAX)];
int getMid(int l, int r) { return l + (r - l) / 2; }
int buildTree(int prev, int val, int idx, int l, int r) {
  if (l <= idx && idx <= r) {
    int cur = ++nodes_ptr;
    if (l == r)
      nodes[cur] = N(0, 0, { val, idx });
    else {
      int mid = getMid(l, r);
      int a = buildTree(nodes[prev].l, val, idx, l, mid);
      int b = buildTree(nodes[prev].r, val, idx, mid + 1, r);
      pii left = (a) ? nodes[a].val : mp(INF, 0);
      pii right = (b) ? nodes[b].val : mp(INF, 0);
      nodes[cur] = N(a, b, min(left, right));
    }
    return cur;
  }
  return prev;
}
pii queryAns(int cur, int sl, int sr, int l, int r) {
  if (!cur || sr < l || r < sl)
    return { INF, 0 };
  if (sl <= l && r <= sr)
    return nodes[cur].val;
  else {
    int mid = getMid(l, r);
    pii x = queryAns(nodes[cur].l, sl, sr, l, mid);
    pii y = queryAns(nodes[cur].r, sl, r, mid + 1, r);
    return min(x, y);
  }
}
void program() {
  cin >> n;
  mii mp;
  for (int i = 1, last_seen; i <= n; ++i) {
    roots[i] = roots[i - 1];
    cin >> in[i];
    last_seen = mp[in[i]] ? mp[in[i]] : -1;
    if (last_seen != -1)
      roots[i] = buildTree(roots[i], INF, last_seen, 1, n);
    roots[i] = buildTree(roots[i], last_seen, i, 1, n);
    mp[in[i]] = i;
  }
  int m;
  cin >> m;
  pii ret;
  for (int i = 1, l, r; i <= m; ++i) {
    cin >> l >> r;
    ret = queryAns(roots[r], l, r, 1, n);
    if (ret.first < l)
      cout << in[ret.second] << endl;
    else cout << "0" << endl;
  }
}
int main() {
  ios::sync_with_stdio(0);
	cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  program();
  return 0;
}


#include<bits/stdc++.h>
using namespace std;

using LL=long long;
using pii=pair<int,int>;
const int MX=500005;
#define cin SELF_DEFINED_ISTREAM
#define cout SELF_DEFINED_OSTREAM
#define endl '\n'

int N,a[MX],B,pos[MX],Q,ans[MX],cnt[MX];
vector<int> S;
bool vis[MX];

struct Ques{int l,r,id,posl;}q[MX];

struct Istream
{
    static const int MXL=2000000;
    char bf[MXL],*p1,*p2;
    FILE *F;

    Istream(FILE *F=stdin):p1(bf),p2(bf),F(F){}
    char getchar(){return p1==p2&&(p2=(p1=bf)+fread(bf,1,MXL,F))==bf?EOF:*p1++;}
    Istream& operator >> (int& x)
    {
        x=0;int c=getchar();
        while(!isdigit(c))c=getchar();
        while(isdigit(c))x=x*10+c-'0',c=getchar();
        return *this;
    }
}cin;

struct Ostream
{
    static const int MXL=200000;
    char bf[MXL],*p1,*const p2;
    int dig[12],top;
    FILE *F;

    Ostream(FILE *F=stdout):p1(bf),p2(bf+MXL),F(F){}
    ~Ostream(){flush();}
    void flush(){fwrite(bf,1,p1-bf,F),p1=bf;}
    void putchar(char c){if(p1==p2)flush();*p1++=c;}
    template<typename T>Ostream& operator << (T x)
    {
        if(typeid(T)==typeid(int))
        {
            top=0;
            do dig[++top]=x%10,x/=10;while(x);
            while(top)putchar(dig[top--]+48);
        }
        else if(typeid(T)==typeid(char))putchar(x);
        return *this;
    }
}cout;

inline void update(int pos,int flag)
{
    int val=a[pos];
    if(cnt[val]==1)vis[val]=true;
    cnt[val]+=flag;
    if(cnt[val]==1)
        if(!vis[val])S.push_back(val);
        else vis[val]=false;
}

int main()
{
    cin>>N,B=sqrt(N)*1.5;
    for(int i=1;i<=N;i++)pos[i]=i/B;
    for(int i=1;i<=N;i++)cin>>a[i];
    cin>>Q;
    for(int i=1,l,r;i<=Q;i++)
    {
        cin>>l>>r;
        q[i]=(Ques){l,r,i,pos[l]};
    }
    sort(q+1,q+Q+1,[](const Ques& a,const Ques& b){return a.posl<b.posl;});
    for(int i=1,j=1,nw=0;i<=Q;i=j,nw^=1)
    {
        while(q[i].posl==q[j].posl&&j<=Q)j++;
        sort(q+i,q+j,[nw](const Ques& a,const Ques& b){return nw?a.r>b.r:a.r<b.r;});
    }
    for(int i=1,l=1,r=0,tmp;i<=Q;i++)
    {
        while(r<q[i].r)update(++r,1);
        while(r>q[i].r)update(r--,-1);
        while(l<q[i].l)update(l++,-1);
        while(l>q[i].l)update(--l,1);
        while(!S.empty()&&vis[tmp=S.back()])vis[tmp]=false,S.pop_back();
        ans[q[i].id]=S.empty()?0:S.back();
    }
    for(int i=1;i<=Q;i++)cout<<ans[i]<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

using LL=long long;
using pii=pair<int,int>;
const int MX=500005;
#define cin SELF_DEFINED_ISTREAM
#define cout SELF_DEFINED_OSTREAM
#define endl '\n'

int N,a[MX],B,pos[MX],Q,ans[MX],cnt[MX];
vector<int> S;
bool vis[MX];

struct Ques{int l,r,id,posl;}q[MX];

struct Istream
{
    static const int MXL=2000000;
    char bf[MXL],*p1,*p2;
    FILE *F;

    Istream(FILE *F=stdin):p1(bf),p2(bf),F(F){}
    char getchar(){return p1==p2&&(p2=(p1=bf)+fread(bf,1,MXL,F))==bf?EOF:*p1++;}
    Istream& operator >> (int& x)
    {
        x=0;int c=getchar();
        while(!isdigit(c))c=getchar();
        while(isdigit(c))x=x*10+c-'0',c=getchar();
        return *this;
    }
}cin;

struct Ostream
{
    static const int MXL=200000;
    char bf[MXL],*p1,*const p2;
    int dig[12],top;
    FILE *F;

    Ostream(FILE *F=stdout):p1(bf),p2(bf+MXL),F(F){}
    ~Ostream(){flush();}
    void flush(){fwrite(bf,1,p1-bf,F),p1=bf;}
    void putchar(char c){if(p1==p2)flush();*p1++=c;}
    template<typename T>Ostream& operator << (T x)
    {
        if(typeid(T)==typeid(int))
        {
            top=0;
            do dig[++top]=x%10,x/=10;while(x);
            while(top)putchar(dig[top--]+48);
        }
        else if(typeid(T)==typeid(char))putchar(x);
        return *this;
    }
}cout;

inline void update(int pos,int flag)
{
    int val=a[pos];
    if(cnt[val]==1)vis[val]=true;
    cnt[val]+=flag;
    if(cnt[val]==1)S.push_back(val),vis[val]=false;
}

int main()
{
    cin>>N,B=sqrt(N)*1.2;
    for(int i=1;i<=N;i++)pos[i]=i/B;
    for(int i=1;i<=N;i++)cin>>a[i];
    cin>>Q;
    for(int i=1,l,r;i<=Q;i++)
    {
        cin>>l>>r;
        q[i]=(Ques){l,r,i,pos[l]};
    }
    sort(q+1,q+Q+1,[](const Ques& a,const Ques& b){return a.posl<b.posl;});
    for(int i=1,j=1,nw=0;i<=Q;i=j,nw^=1)
    {
        while(q[i].posl==q[j].posl&&j<=Q)j++;
        sort(q+i,q+j,[nw](const Ques& a,const Ques& b){return nw?a.r>b.r:a.r<b.r;});
    }
    for(int i=1,l=1,r=0,tmp;i<=Q;i++)
    {
        while(r<q[i].r)update(++r,1);
        while(r>q[i].r)update(r--,-1);
        while(l<q[i].l)update(l++,-1);
        while(l>q[i].l)update(--l,1);
        while(!S.empty()&&vis[tmp=S.back()])vis[tmp]=false,S.pop_back();
        ans[q[i].id]=S.empty()?0:S.back();
    }
    for(int i=1;i<=Q;i++)cout<<ans[i]<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

using LL=long long;
using pii=pair<int,int>;
const int MX=500005;
#define cin SELF_DEFINED_ISTREAM
#define cout SELF_DEFINED_OSTREAM
#define endl '\n'

int N,a[MX],B,pos[MX],Q,ans[MX],cnt[MX];
vector<int> S;
bool vis[MX];

struct Ques{int l,r,id,posl;}q[MX];

struct Istream
{
    static const int MXL=2000000;
    char bf[MXL],*p1,*p2;
    FILE *F;

    Istream(FILE *F=stdin):p1(bf),p2(bf),F(F){}
    char getchar(){return p1==p2&&(p2=(p1=bf)+fread(bf,1,MXL,F))==bf?EOF:*p1++;}
    Istream& operator >> (int& x)
    {
        x=0;int c=getchar();
        while(!isdigit(c))c=getchar();
        while(isdigit(c))x=x*10+c-'0',c=getchar();
        return *this;
    }
}cin;

struct Ostream
{
    static const int MXL=200000;
    char bf[MXL],*p1,*const p2;
    int dig[12],top;
    FILE *F;

    Ostream(FILE *F=stdout):p1(bf),p2(bf+MXL),F(F){}
    ~Ostream(){flush();}
    void flush(){fwrite(bf,1,p1-bf,F),p1=bf;}
    void putchar(char c){if(p1==p2)flush();*p1++=c;}
    template<typename T>Ostream& operator << (T x)
    {
        if(typeid(T)==typeid(int))
        {
            top=0;
            do dig[++top]=x%10,x/=10;while(x);
            while(top)putchar(dig[top--]+48);
        }
        else if(typeid(T)==typeid(char))putchar(x);
        return *this;
    }
}cout;

inline void update(int pos,int flag)
{
    int val=a[pos];
    if(cnt[val]==1)vis[val]=true;
    cnt[val]+=flag;
    if(cnt[val]==1)
        if(!vis[val])S.push_back(val);
        else vis[val]=false;
}

int main()
{
    cin>>N,B=sqrt(N)*2.2;
    for(int i=1;i<=N;i++)pos[i]=i/B;
    for(int i=1;i<=N;i++)cin>>a[i];
    cin>>Q;
    for(int i=1,l,r;i<=Q;i++)
    {
        cin>>l>>r;
        q[i]=(Ques){l,r,i,pos[l]};
    }
    sort(q+1,q+Q+1,[](const Ques& a,const Ques& b){return a.posl<b.posl;});
    for(int i=1,j=1,nw=0;i<=Q;i=j,nw^=1)
    {
        while(q[i].posl==q[j].posl&&j<=Q)j++;
        sort(q+i,q+j,[nw](const Ques& a,const Ques& b){return nw?a.r>b.r:a.r<b.r;});
    }
    for(int i=1,l=1,r=0,tmp;i<=Q;i++)
    {
        while(r<q[i].r)update(++r,1);
        while(r>q[i].r)update(r--,-1);
        while(l<q[i].l)update(l++,-1);
        while(l>q[i].l)update(--l,1);
        while(!S.empty()&&vis[tmp=S.back()])vis[tmp]=false,S.pop_back();
        ans[q[i].id]=S.empty()?0:S.back();
    }
    for(int i=1;i<=Q;i++)cout<<ans[i]<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

using LL=long long;
using pii=pair<int,int>;
const int MX=500005;
#define cin SELF_DEFINED_ISTREAM
#define cout SELF_DEFINED_OSTREAM
#define endl '\n'

int N,a[MX],B,pos[MX],Q,ans[MX],cnt[MX],P[MX],*S=P;
bool vis[MX];

struct Ques{int l,r,id,posl;}q[MX];

struct Istream
{
    static const int MXL=2000000;
    char bf[MXL],*p1,*p2;
    FILE *F;

    Istream(FILE *F=stdin):p1(bf),p2(bf),F(F){}
    char getchar(){return p1==p2&&(p2=(p1=bf)+fread(bf,1,MXL,F))==bf?EOF:*p1++;}
    Istream& operator >> (int& x)
    {
        x=0;int c=getchar();
        while(!isdigit(c))c=getchar();
        while(isdigit(c))x=x*10+c-'0',c=getchar();
        return *this;
    }
}cin;

struct Ostream
{
    static const int MXL=200000;
    char bf[MXL],*p1,*const p2;
    int dig[12],top;
    FILE *F;

    Ostream(FILE *F=stdout):p1(bf),p2(bf+MXL),F(F){}
    ~Ostream(){flush();}
    void flush(){fwrite(bf,1,p1-bf,F),p1=bf;}
    void putchar(char c){if(p1==p2)flush();*p1++=c;}
    template<typename T>Ostream& operator << (T x)
    {
        if(typeid(T)==typeid(int))
        {
            top=0;
            do dig[++top]=x%10,x/=10;while(x);
            while(top)putchar(dig[top--]+48);
        }
        else if(typeid(T)==typeid(char))putchar(x);
        return *this;
    }
}cout;

inline void update(int pos,int flag)
{
    int val=a[pos];
    if(cnt[val]==1)vis[val]=true;
    cnt[val]+=flag;
    if(cnt[val]==1)
        if(!vis[val])*++S=val;
        else vis[val]=false;
}

int main()
{
    cin>>N,B=sqrt(N)*1.816;
    for(int i=1;i<=N;i++)pos[i]=i/B;
    for(int i=1;i<=N;i++)cin>>a[i];
    cin>>Q;
    for(int i=1,l,r;i<=Q;i++)
    {
        cin>>l>>r;
        q[i]=(Ques){l,r,i,pos[l]};
    }
    sort(q+1,q+Q+1,[](const Ques& a,const Ques& b){return a.posl<b.posl;});
    for(int i=1,j=1,nw=0;i<=Q;i=j,nw^=1)
    {
        while(q[i].posl==q[j].posl&&j<=Q)j++;
        sort(q+i,q+j,[nw](const Ques& a,const Ques& b){return nw?a.r>b.r:a.r<b.r;});
    }
    for(int i=1,l=1,r=0,tmp;i<=Q;i++)
    {
        while(r<q[i].r)update(++r,1);
        while(r>q[i].r)update(r--,-1);
        while(l<q[i].l)update(l++,-1);
        while(l>q[i].l)update(--l,1);
        while(S!=P&&vis[tmp=*S])vis[tmp]=false,--S;
        ans[q[i].id]=S==P?0:*S;
    }
    for(int i=1;i<=Q;i++)cout<<ans[i]<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

using LL=long long;
using pii=pair<int,int>;
const int MX=500005;
#define cin SELF_DEFINED_ISTREAM
#define cout SELF_DEFINED_OSTREAM
#define endl '\n'

int N,a[MX],B,pos[MX],Q,ans[MX],cnt[MX];
vector<int> S;
bool vis[MX];

struct Ques{int l,r,id,posl;}q[MX];

struct Istream
{
    static const int MXL=2000000;
    char bf[MXL],*p1,*p2;
    FILE *F;

    Istream(FILE *F=stdin):p1(bf),p2(bf),F(F){}
    char getchar(){return p1==p2&&(p2=(p1=bf)+fread(bf,1,MXL,F))==bf?EOF:*p1++;}
    Istream& operator >> (int& x)
    {
        x=0;int c=getchar();
        while(!isdigit(c))c=getchar();
        while(isdigit(c))x=x*10+c-'0',c=getchar();
        return *this;
    }
}cin;

struct Ostream
{
    static const int MXL=200000;
    char bf[MXL],*p1,*const p2;
    int dig[12],top;
    FILE *F;

    Ostream(FILE *F=stdout):p1(bf),p2(bf+MXL),F(F){}
    ~Ostream(){flush();}
    void flush(){fwrite(bf,1,p1-bf,F),p1=bf;}
    void putchar(char c){if(p1==p2)flush();*p1++=c;}
    template<typename T>Ostream& operator << (T x)
    {
        if(typeid(T)==typeid(int))
        {
            top=0;
            do dig[++top]=x%10,x/=10;while(x);
            while(top)putchar(dig[top--]+48);
        }
        else if(typeid(T)==typeid(char))putchar(x);
        return *this;
    }
}cout;

inline void update(int pos,int flag)
{
    int val=a[pos];
    if(cnt[val]==1)vis[val]=true;
    cnt[val]+=flag;
    if(cnt[val]==1)
        if(!vis[val])S.push_back(val);
        else vis[val]=false;
}

int main()
{
    cin>>N,B=sqrt(N)*1.816;
    for(int i=1;i<=N;i++)pos[i]=i/B;
    for(int i=1;i<=N;i++)cin>>a[i];
    cin>>Q;
    for(int i=1,l,r;i<=Q;i++)
    {
        cin>>l>>r;
        q[i]=(Ques){l,r,i,pos[l]};
    }
    sort(q+1,q+Q+1,[](const Ques& a,const Ques& b){return a.posl<b.posl;});
    for(int i=1,j=1,nw=0;i<=Q;i=j,nw^=1)
    {
        while(q[i].posl==q[j].posl&&j<=Q)j++;
        sort(q+i,q+j,[nw](const Ques& a,const Ques& b){return nw?a.r>b.r:a.r<b.r;});
    }
    for(int i=1,l=1,r=0,tmp;i<=Q;i++)
    {
        while(r<q[i].r)update(++r,1);
        while(r>q[i].r)update(r--,-1);
        while(l<q[i].l)update(l++,-1);
        while(l>q[i].l)update(--l,1);
        while(!S.empty()&&vis[tmp=S.back()])vis[tmp]=false,S.pop_back();
        ans[q[i].id]=S.empty()?0:S.back();
    }
    for(int i=1;i<=Q;i++)cout<<ans[i]<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

using LL=long long;
using pii=pair<int,int>;
const int MX=500005;
#define cin SELF_DEFINED_ISTREAM
#define cout SELF_DEFINED_OSTREAM
#define endl '\n'

int N,a[MX],B,pos[MX],Q,ans[MX],cnt[MX];
unordered_set<int> S;

struct Ques{int l,r,id,posl;}q[MX];

struct Istream
{
    static const int MXL=2000000;
    char bf[MXL],*p1,*p2;
    FILE *F;

    Istream(FILE *F=stdin):p1(bf),p2(bf),F(F){}
    char getchar(){return p1==p2&&(p2=(p1=bf)+fread(bf,1,MXL,F))==bf?EOF:*p1++;}
    Istream& operator >> (int& x)
    {
        x=0;int c=getchar();
        while(!isdigit(c))c=getchar();
        while(isdigit(c))x=x*10+c-'0',c=getchar();
        return *this;
    }
}cin;

struct Ostream
{
    static const int MXL=200000;
    char bf[MXL],*p1,*const p2;
    int dig[12],top;
    FILE *F;

    Ostream(FILE *F=stdout):p1(bf),p2(bf+MXL),F(F){}
    ~Ostream(){flush();}
    void flush(){fwrite(bf,1,p1-bf,F),p1=bf;}
    void putchar(char c){if(p1==p2)flush();*p1++=c;}
    template<typename T>Ostream& operator << (T x)
    {
        if(typeid(T)==typeid(int))
        {
            top=0;
            do dig[++top]=x%10,x/=10;while(x);
            while(top)putchar(dig[top--]+48);
        }
        else if(typeid(T)==typeid(char))putchar(x);
        return *this;
    }
}cout;

inline void update(int pos,int flag)
{
    int val=a[pos];
    if(flag==1)
    {
        cnt[val]++;
        if(cnt[val]==1)S.insert(val);
        if(cnt[val]==2)S.erase(val);
    }
    if(flag==-1)
    {
        cnt[val]--;
        if(cnt[val]==1)S.insert(val);
        if(cnt[val]==0)S.erase(val);
    }
}

int main()
{
    cin>>N,B=sqrt(N)*1.2;
    for(int i=1;i<=N;i++)pos[i]=i/B;
    for(int i=1;i<=N;i++)cin>>a[i];
    cin>>Q;
    for(int i=1,l,r;i<=Q;i++)
    {
        cin>>l>>r;
        q[i]=(Ques){l,r,i,pos[l]};
    }
    sort(q+1,q+Q+1,[](const Ques& a,const Ques& b){return a.posl<b.posl;});
    for(int i=1,j=1,nw=0;i<=Q;i=j,nw^=1)
    {
        while(q[i].posl==q[j].posl&&j<=Q)j++;
        sort(q+i,q+j,[nw](const Ques& a,const Ques& b){return nw?a.r>b.r:a.r<b.r;});
    }
    for(int i=1,l=1,r=0;i<=Q;i++)
    {
        while(r<q[i].r)update(++r,1);
        while(r>q[i].r)update(r--,-1);
        while(l<q[i].l)update(l++,-1);
        while(l>q[i].l)update(--l,1);
        ans[q[i].id]=S.size()?*S.begin():0;
    }
    for(int i=1;i<=Q;i++)cout<<ans[i]<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

using LL=long long;
using pii=pair<int,int>;
const int MX=500005;
#define cin SELF_DEFINED_ISTREAM
#define cout SELF_DEFINED_OSTREAM
#define endl '\n'

int N,a[MX],B,pos[MX],Q,ans[MX],cnt[MX];
vector<int> S;
bool vis[MX];

struct Ques{int l,r,id,posl;}q[MX];

struct Istream
{
    static const int MXL=2000000;
    char bf[MXL],*p1,*p2;
    FILE *F;

    Istream(FILE *F=stdin):p1(bf),p2(bf),F(F){}
    char getchar(){return p1==p2&&(p2=(p1=bf)+fread(bf,1,MXL,F))==bf?EOF:*p1++;}
    Istream& operator >> (int& x)
    {
        x=0;int c=getchar();
        while(!isdigit(c))c=getchar();
        while(isdigit(c))x=x*10+c-'0',c=getchar();
        return *this;
    }
}cin;

struct Ostream
{
    static const int MXL=200000;
    char bf[MXL],*p1,*const p2;
    int dig[12],top;
    FILE *F;

    Ostream(FILE *F=stdout):p1(bf),p2(bf+MXL),F(F){}
    ~Ostream(){flush();}
    void flush(){fwrite(bf,1,p1-bf,F),p1=bf;}
    void putchar(char c){if(p1==p2)flush();*p1++=c;}
    template<typename T>Ostream& operator << (T x)
    {
        if(typeid(T)==typeid(int))
        {
            top=0;
            do dig[++top]=x%10,x/=10;while(x);
            while(top)putchar(dig[top--]+48);
        }
        else if(typeid(T)==typeid(char))putchar(x);
        return *this;
    }
}cout;

inline void update(int pos,int flag)
{
    int val=a[pos];
    if(cnt[val]==1)vis[val]=true;
    cnt[val]+=flag;
    if(cnt[val]==1)S.push_back(val);
}

int main()
{
    cin>>N,B=sqrt(N)*1.2;
    for(int i=1;i<=N;i++)pos[i]=i/B;
    for(int i=1;i<=N;i++)cin>>a[i];
    cin>>Q;
    for(int i=1,l,r;i<=Q;i++)
    {
        cin>>l>>r;
        q[i]=(Ques){l,r,i,pos[l]};
    }
    sort(q+1,q+Q+1,[](const Ques& a,const Ques& b){return a.posl<b.posl;});
    for(int i=1,j=1,nw=0;i<=Q;i=j,nw^=1)
    {
        while(q[i].posl==q[j].posl&&j<=Q)j++;
        sort(q+i,q+j,[nw](const Ques& a,const Ques& b){return nw?a.r>b.r:a.r<b.r;});
    }
    for(int i=1,l=1,r=0,tmp;i<=Q;i++)
    {
        while(r<q[i].r)update(++r,1);
        while(r>q[i].r)update(r--,-1);
        while(l<q[i].l)update(l++,-1);
        while(l>q[i].l)update(--l,1);
        while(!S.empty()&&vis[tmp=S.back()])vis[tmp]=false,S.pop_back();
        ans[q[i].id]=S.empty()?0:S.back();
    }
    for(int i=1;i<=Q;i++)cout<<ans[i]<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

using LL=long long;
using pii=pair<int,int>;
const int MX=500005;
#define cin SELF_DEFINED_ISTREAM
#define cout SELF_DEFINED_OSTREAM
#define endl '\n'

int N,a[MX],B,pos[MX],Q,ans[MX],cnt[MX];
vector<int> S;
bool vis[MX];

struct Ques{int l,r,id,posl;}q[MX];

struct Istream
{
    static const int MXL=2000000;
    char bf[MXL],*p1,*p2;
    FILE *F;

    Istream(FILE *F=stdin):p1(bf),p2(bf),F(F){}
    char getchar(){return p1==p2&&(p2=(p1=bf)+fread(bf,1,MXL,F))==bf?EOF:*p1++;}
    Istream& operator >> (int& x)
    {
        x=0;int c=getchar();
        while(!isdigit(c))c=getchar();
        while(isdigit(c))x=x*10+c-'0',c=getchar();
        return *this;
    }
}cin;

struct Ostream
{
    static const int MXL=200000;
    char bf[MXL],*p1,*const p2;
    int dig[12],top;
    FILE *F;

    Ostream(FILE *F=stdout):p1(bf),p2(bf+MXL),F(F){}
    ~Ostream(){flush();}
    void flush(){fwrite(bf,1,p1-bf,F),p1=bf;}
    void putchar(char c){if(p1==p2)flush();*p1++=c;}
    template<typename T>Ostream& operator << (T x)
    {
        if(typeid(T)==typeid(int))
        {
            top=0;
            do dig[++top]=x%10,x/=10;while(x);
            while(top)putchar(dig[top--]+48);
        }
        else if(typeid(T)==typeid(char))putchar(x);
        return *this;
    }
}cout;

inline void update(int pos,int flag)
{
    int val=a[pos];
    if(cnt[val]==1)vis[val]=true;
    cnt[val]+=flag;
    if(cnt[val]==1)
        if(!vis[val])S.push_back(val);
        else vis[val]=false;
}

int main()
{
    cin>>N,B=sqrt(N)*1.2;
    for(int i=1;i<=N;i++)pos[i]=i/B;
    for(int i=1;i<=N;i++)cin>>a[i];
    cin>>Q;
    for(int i=1,l,r;i<=Q;i++)
    {
        cin>>l>>r;
        q[i]=(Ques){l,r,i,pos[l]};
    }
    sort(q+1,q+Q+1,[](const Ques& a,const Ques& b){return a.posl<b.posl;});
    for(int i=1,j=1,nw=0;i<=Q;i=j,nw^=1)
    {
        while(q[i].posl==q[j].posl&&j<=Q)j++;
        sort(q+i,q+j,[nw](const Ques& a,const Ques& b){return nw?a.r>b.r:a.r<b.r;});
    }
    for(int i=1,l=1,r=0,tmp;i<=Q;i++)
    {
        while(r<q[i].r)update(++r,1);
        while(r>q[i].r)update(r--,-1);
        while(l<q[i].l)update(l++,-1);
        while(l>q[i].l)update(--l,1);
        while(!S.empty()&&vis[tmp=S.back()])vis[tmp]=false,S.pop_back();
        ans[q[i].id]=S.empty()?0:S.back();
    }
    for(int i=1;i<=Q;i++)cout<<ans[i]<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

using LL=long long;
using pii=pair<int,int>;
const int MX=500005;
#define cin SELF_DEFINED_ISTREAM
#define cout SELF_DEFINED_OSTREAM
#define endl '\n'

int N,a[MX],B,pos[MX],Q,ans[MX],cnt[MX];
unordered_set<int> S;

struct Ques{int l,r,id,posl;}q[MX];

struct Istream
{
    static const int MXL=2000000;
    char bf[MXL],*p1,*p2;
    FILE *F;

    Istream(FILE *F=stdin):p1(bf),p2(bf),F(F){}
    char getchar(){return p1==p2&&(p2=(p1=bf)+fread(bf,1,MXL,F))==bf?EOF:*p1++;}
    Istream& operator >> (int& x)
    {
        x=0;int c=getchar();
        while(!isdigit(c))c=getchar();
        while(isdigit(c))x=x*10+c-'0',c=getchar();
        return *this;
    }
}cin;

struct Ostream
{
    static const int MXL=200000;
    char bf[MXL],*p1,*const p2;
    int dig[12],top;
    FILE *F;

    Ostream(FILE *F=stdout):p1(bf),p2(bf+MXL),F(F){}
    ~Ostream(){flush();}
    void flush(){fwrite(bf,1,p1-bf,F),p1=bf;}
    void putchar(char c){if(p1==p2)flush();*p1++=c;}
    template<typename T>Ostream& operator << (T x)
    {
        if(typeid(T)==typeid(int))
        {
            dig[top=1]=48;
            do dig[++top]=x%10,x/=10;while(x);
            while(top)putchar(dig[top--]+48);
        }
        else if(typeid(T)==typeid(char))putchar(x);
        return *this;
    }
}cout;

inline void update(int pos,int flag)
{
    int val=a[pos];
    if(flag==1)
    {
        cnt[val]++;
        if(cnt[val]==1)S.insert(val);
        if(cnt[val]==2)S.erase(val);
    }
    if(flag==-1)
    {
        cnt[val]--;
        if(cnt[val]==1)S.insert(val);
        if(cnt[val]==0)S.erase(val);
    }
}

int main()
{
    cin>>N,B=sqrt(N);
    for(int i=1;i<=N;i++)pos[i]=i/B;
    for(int i=1;i<=N;i++)cin>>a[i];
    cin>>Q;
    for(int i=1,l,r;i<=Q;i++)
    {
        cin>>l>>r;
        q[i]=(Ques){l,r,i,pos[l]};
    }
    sort(q+1,q+Q+1,[](const Ques& a,const Ques& b){return a.posl<b.posl;});
    for(int i=1,j=1,nw=0;i<=Q;i=j,nw^=1)
    {
        while(q[i].posl==q[j].posl&&j<=Q)j++;
        sort(q+i,q+j,[nw](const Ques& a,const Ques& b){return nw?a.r>b.r:a.r<b.r;});
    }
    for(int i=1,l=1,r=0;i<=Q;i++)
    {
        while(r<q[i].r)update(++r,1);
        while(r>q[i].r)update(r--,-1);
        while(l<q[i].l)update(l++,-1);
        while(l>q[i].l)update(--l,1);
        ans[q[i].id]=S.size()?*S.begin():0;
    }
    for(int i=1;i<=Q;i++)cout<<ans[i]<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

using LL=long long;
using pii=pair<int,int>;
const int MX=500005;
#define cin SELF_DEFINED_ISTREAM
#define cout SELF_DEFINED_OSTREAM
#define endl '\n'

int N,a[MX],B,pos[MX],Q,ans[MX],cnt[MX];
unordered_set<int> S;

struct Ques{int l,r,id,posl;}q[MX];

struct Istream
{
    static const int MXL=2000000;
    char bf[MXL],*p1,*p2;
    FILE *F;

    Istream(FILE *F=stdin):p1(bf),p2(bf),F(F){}
    char getchar(){return p1==p2&&(p2=(p1=bf)+fread(bf,1,MXL,F))==bf?EOF:*p1++;}
    Istream& operator >> (int& x)
    {
        x=0;int c=getchar();
        while(!isdigit(c))c=getchar();
        while(isdigit(c))x=x*10+c-'0',c=getchar();
        return *this;
    }
}cin;

struct Ostream
{
    static const int MXL=200000;
    char bf[MXL],*p1,*const p2;
    int dig[12],top;
    FILE *F;

    Ostream(FILE *F=stdout):p1(bf),p2(bf+MXL),F(F){}
    ~Ostream(){flush();}
    void flush(){fwrite(bf,1,p1-bf,F),p1=bf;}
    void putchar(char c){if(p1==p2)flush();*p1++=c;}
    template<typename T>Ostream& operator << (T x)
    {
        if(typeid(T)==typeid(int))
        {
            top=0;
            do dig[++top]=x%10,x/=10;while(x);
            while(top)putchar(dig[top--]+48);
        }
        else if(typeid(T)==typeid(char))putchar(x);
        return *this;
    }
}cout;

inline void update(int pos,int flag)
{
    int val=a[pos];
    if(flag==1)
    {
        cnt[val]++;
        if(cnt[val]==1)S.insert(val);
        if(cnt[val]==2)S.erase(val);
    }
    if(flag==-1)
    {
        cnt[val]--;
        if(cnt[val]==1)S.insert(val);
        if(cnt[val]==0)S.erase(val);
    }
}

int main()
{
    cin>>N,B=sqrt(N);
    for(int i=1;i<=N;i++)pos[i]=i/B;
    for(int i=1;i<=N;i++)cin>>a[i];
    cin>>Q;
    for(int i=1,l,r;i<=Q;i++)
    {
        cin>>l>>r;
        q[i]=(Ques){l,r,i,pos[l]};
    }
    sort(q+1,q+Q+1,[](const Ques& a,const Ques& b){return a.posl<b.posl;});
    for(int i=1,j=1,nw=0;i<=Q;i=j,nw^=1)
    {
        while(q[i].posl==q[j].posl&&j<=Q)j++;
        sort(q+i,q+j,[nw](const Ques& a,const Ques& b){return nw?a.r>b.r:a.r<b.r;});
    }
    for(int i=1,l=1,r=0;i<=Q;i++)
    {
        while(r<q[i].r)update(++r,1);
        while(r>q[i].r)update(r--,-1);
        while(l<q[i].l)update(l++,-1);
        while(l>q[i].l)update(--l,1);
        ans[q[i].id]=S.size()?*S.begin():0;
    }
    for(int i=1;i<=Q;i++)cout<<ans[i]<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

using LL=long long;
using pii=pair<int,int>;
const int MX=500005;

int N,a[MX],B,pos[MX],Q,ans[MX],cnt[MX];
unordered_set<int> S;

struct Ques
{
    int l,r,id;
    bool operator < (const Ques& a)const{return pos[l]==pos[a.l]?r<a.r:pos[l]<pos[a.l];}
}q[MX];

inline int read()
{
    int x=0,c=getchar();
    while(!isdigit(c))c=getchar();
    while(isdigit(c))x=x*10+c-'0',c=getchar();
    return x;
}
inline void update(int pos,int flag)
{
    int val=a[pos];
    if(flag==1)
    {
        cnt[val]++;
        if(cnt[val]==1)S.insert(val);
        if(cnt[val]==2)S.erase(val);
    }
    if(flag==-1)
    {
        cnt[val]--;
        if(cnt[val]==1)S.insert(val);
        if(cnt[val]==0)S.erase(val);
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("F.in","r",stdin);
    freopen("F.out","w",stdout);
#endif
    N=read(),B=max(sqrt(N)/2.5,1.0);
    for(int i=1;i<=N;i++)pos[i]=i/B;
    for(int i=1;i<=N;i++)a[i]=read();
    Q=read();
    for(int i=1;i<=Q;i++)
    {
        int l=read(),r=read();
        q[i]=(Ques){l,r,i};
    }
    sort(q+1,q+Q+1);
    for(int i=1,l=1,r=0;i<=Q;i++)
    {
        while(r<q[i].r)update(++r,1);
        while(r>q[i].r)update(r--,-1);
        while(l<q[i].l)update(l++,-1);
        while(l>q[i].l)update(--l,1);
        ans[q[i].id]=S.size()?*S.begin():0;
    }
    for(int i=1;i<=Q;i++)printf("%d\n",ans[i]);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

using LL=long long;
using pii=pair<int,int>;
const int MX=500005;

int N,a[MX],B,pos[MX],Q,ans[MX],cnt[MX];
unordered_set<int> S;

struct Ques
{
    int l,r,id;
    bool operator < (const Ques& a)const{return pos[l]==pos[a.l]?r<a.r:pos[l]<pos[a.l];}
}q[MX];

inline int read()
{
    int x=0,c=getchar();
    while(!isdigit(c))c=getchar();
    while(isdigit(c))x=x*10+c-'0',c=getchar();
    return x;
}
void update(int pos,int flag)
{
    int val=a[pos];
    if(flag==1)
    {
        cnt[val]++;
        if(cnt[val]==1)S.insert(val);
        if(cnt[val]==2)S.erase(val);
    }
    if(flag==-1)
    {
        cnt[val]--;
        if(cnt[val]==1)S.insert(val);
        if(cnt[val]==0)S.erase(val);
    }
}

int main()
{
    N=read(),B=sqrt(N);
    for(int i=1;i<=N;i++)pos[i]=i/B;
    for(int i=1;i<=N;i++)a[i]=read();
    Q=read();
    for(int i=1;i<=Q;i++)
    {
        int l=read(),r=read();
        q[i]=(Ques){l,r,i};
    }
    sort(q+1,q+Q+1);
    for(int i=1,l=1,r=0;i<=Q;i++)
    {
        while(r<q[i].r)update(++r,1);
        while(r>q[i].r)update(r--,-1);
        while(l<q[i].l)update(l++,-1);
        while(l>q[i].l)update(--l,1);
        ans[q[i].id]=S.size()?*S.begin():0;
    }
    for(int i=1;i<=Q;i++)printf("%d\n",ans[i]);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

using LL=long long;
using pii=pair<int,int>;
const int MX=500005;
#define cin SELF_DEFINED_ISTREAM
#define cout SELF_DEFINED_OSTREAM
#define endl '\n'

int N,a[MX],B,pos[MX],Q,ans[MX],cnt[MX];
unordered_set<int> S;

struct Ques{int l,r,id,posl;}q[MX];

struct Istream
{
    static const int MXL=2000000;
    char bf[MXL],*p1,*p2;
    FILE *F;

    Istream(FILE *F=stdin):p1(bf),p2(bf),F(F){}
    char getchar(){return p1==p2&&(p2=(p1=bf)+fread(bf,1,MXL,F))==bf?EOF:*p1++;}
    Istream& operator >> (int& x)
    {
        x=0;int c=getchar();
        while(!isdigit(c))c=getchar();
        while(isdigit(c))x=x*10+c-'0',c=getchar();
        return *this;
    }
}cin;

struct Ostream
{
    static const int MXL=200000;
    char bf[MXL],*p1,*const p2;
    int dig[12],top;
    FILE *F;

    Ostream(FILE *F=stdout):p1(bf),p2(bf+MXL),F(F){}
    ~Ostream(){flush();}
    void flush(){fwrite(bf,1,p1-bf,F),p1=bf;}
    void putchar(char c){if(p1==p2)flush();*p1++=c;}
    template<typename T>Ostream& operator << (T x)
    {
        if(typeid(T)==typeid(int))
        {
            dig[top=1]=48;
            do dig[++top]=x%10,x/=10;while(x);
            while(top)putchar(dig[top--]+48);
        }
        else if(typeid(T)==typeid(char))putchar(x);
        return *this;
    }
}cout;

inline void update(int pos,int flag)
{
    int val=a[pos];
    if(flag==1)
    {
        cnt[val]++;
        if(cnt[val]==1)S.insert(val);
        if(cnt[val]==2)S.erase(val);
    }
    if(flag==-1)
    {
        cnt[val]--;
        if(cnt[val]==1)S.insert(val);
        if(cnt[val]==0)S.erase(val);
    }
}

int main()
{
    cin>>N,B=sqrt(N)*1.2;
    for(int i=1;i<=N;i++)pos[i]=i/B;
    for(int i=1;i<=N;i++)cin>>a[i];
    cin>>Q;
    for(int i=1,l,r;i<=Q;i++)
    {
        cin>>l>>r;
        q[i]=(Ques){l,r,i,pos[l]};
    }
    sort(q+1,q+Q+1,[](const Ques& a,const Ques& b){return a.posl<b.posl;});
    for(int i=1,j=1,nw=0;i<=Q;i=j,nw^=1)
    {
        while(q[i].posl==q[j].posl&&j<=Q)j++;
        sort(q+i,q+j,[nw](const Ques& a,const Ques& b){return nw?a.r>b.r:a.r<b.r;});
    }
    for(int i=1,l=1,r=0;i<=Q;i++)
    {
        while(r<q[i].r)update(++r,1);
        while(r>q[i].r)update(r--,-1);
        while(l<q[i].l)update(l++,-1);
        while(l>q[i].l)update(--l,1);
        ans[q[i].id]=S.size()?*S.begin():0;
    }
    for(int i=1;i<=Q;i++)cout<<ans[i]<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

using LL=long long;
using pii=pair<int,int>;
const int MX=500005;
#define cin SELF_DEFINED_ISTREAM
#define cout SELF_DEFINED_OSTREAM
#define endl '\n'

int N,a[MX],B,pos[MX],Q,ans[MX],cnt[MX];
vector<int> S;
bool vis[MX];

struct Ques{int l,r,id,posl;}q[MX];

struct Istream
{
    static const int MXL=2000000;
    char bf[MXL],*p1,*p2;
    FILE *F;

    Istream(FILE *F=stdin):p1(bf),p2(bf),F(F){}
    char getchar(){return p1==p2&&(p2=(p1=bf)+fread(bf,1,MXL,F))==bf?EOF:*p1++;}
    Istream& operator >> (int& x)
    {
        x=0;int c=getchar();
        while(!isdigit(c))c=getchar();
        while(isdigit(c))x=x*10+c-'0',c=getchar();
        return *this;
    }
}cin;

struct Ostream
{
    static const int MXL=200000;
    char bf[MXL],*p1,*const p2;
    int dig[12],top;
    FILE *F;

    Ostream(FILE *F=stdout):p1(bf),p2(bf+MXL),F(F){}
    ~Ostream(){flush();}
    void flush(){fwrite(bf,1,p1-bf,F),p1=bf;}
    void putchar(char c){if(p1==p2)flush();*p1++=c;}
    template<typename T>Ostream& operator << (T x)
    {
        if(typeid(T)==typeid(int))
        {
            top=0;
            do dig[++top]=x%10,x/=10;while(x);
            while(top)putchar(dig[top--]+48);
        }
        else if(typeid(T)==typeid(char))putchar(x);
        return *this;
    }
}cout;

inline void update(int pos,int flag)
{
    int val=a[pos];
    if(cnt[val]==1)vis[val]=true;
    cnt[val]+=flag;
    if(cnt[val]==1&&!vis[val])S.push_back(val);
}

int main()
{
    cin>>N,B=sqrt(N)*1.2;
    for(int i=1;i<=N;i++)pos[i]=i/B;
    for(int i=1;i<=N;i++)cin>>a[i];
    cin>>Q;
    for(int i=1,l,r;i<=Q;i++)
    {
        cin>>l>>r;
        q[i]=(Ques){l,r,i,pos[l]};
    }
    sort(q+1,q+Q+1,[](const Ques& a,const Ques& b){return a.posl<b.posl;});
    for(int i=1,j=1,nw=0;i<=Q;i=j,nw^=1)
    {
        while(q[i].posl==q[j].posl&&j<=Q)j++;
        sort(q+i,q+j,[nw](const Ques& a,const Ques& b){return nw?a.r>b.r:a.r<b.r;});
    }
    for(int i=1,l=1,r=0,tmp;i<=Q;i++)
    {
        while(r<q[i].r)update(++r,1);
        while(r>q[i].r)update(r--,-1);
        while(l<q[i].l)update(l++,-1);
        while(l>q[i].l)update(--l,1);
        while(!S.empty()&&vis[tmp=S.back()])vis[tmp]=false,S.pop_back();
        ans[q[i].id]=S.empty()?0:S.back();
    }
    for(int i=1;i<=Q;i++)cout<<ans[i]<<endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>

char _buf[1 << 16];
int _pos = 1;
int _cap = 1;

char _obuf[1 << 16];
int _opos = 0;

unsigned char* _abuf;
int _apos = 1 << 20;

inline char nextChar()
{
	if (_cap == 0) return 0;
	if (_pos == _cap)
	{
		_cap = (int)fread(_buf, 1, 1 << 16, stdin);
		if (_cap == 0) return 0;
		_pos = 0;
	}
	return _buf[_pos++];
}

inline int nextInt()
{
	char c = nextChar();
	while (isspace(c)) c = nextChar();
	int res = 0;
	while (isdigit(c))
	{
		res = res * 10 + c - '0';
		c = nextChar();
	}
	return res;
}

char tmp;
inline void putInt(int x)
{
	if (x < 0)
	{
		_obuf[_opos++] = '-';
		x = -x;
	}
	int cur = _opos;
	while (x >= 10)
	{
		_obuf[_opos++] = (char)(x % 10 + '0');
		x /= 10;
	}
	_obuf[_opos++] = (char)(x + '0');
	for (int i = cur, j = _opos - 1; i < j; ++i, --j)
	{
	    tmp=_obuf[i];
	    _obuf[i]=_obuf[j];
	    _obuf[j]=tmp;
	}
	if (_opos > 64000)
	{
		fwrite(_obuf, 1, _opos, stdout);
		_opos = 0;
	}
}
inline void putChar(char x)
{
	_obuf[_opos++] = x;
	if (_opos > 64000)
	{
		fwrite(_obuf, 1, _opos, stdout);
		_opos = 0;
	}
}

inline void flushInt()
{
	if (_opos)  fwrite(_obuf, 1, _opos, stdout);
	fflush(stdout);
}

using namespace std;
const int N = 500500;
struct node
{
    int l,r,id;
}   que[N];
int a[N],c[N];
bool vis[N];
int n,m;
int b;
int ans[N];

inline bool cmp(const node &x,const node &y)
{
    if (x.l/b<y.l/b)
        return 1;
    if (x.l/b>y.l/b)
        return 0;
    return ((x.l/b)&1)?x.r<y.r:x.r>y.r;
}

vector<int> answer;

inline void op(int x)
{
	if(c[x]==1 && !vis[x])answer.push_back(x),vis[x]=1;
}

inline void add(int x)
{
    c[a[x]]++;
	if(c[a[x]]==1 && !vis[a[x]])answer.push_back(a[x]),vis[a[x]]=1;
  //  op(a[x]);
}
inline void del(int x)
{
    c[a[x]]--;
	if(c[a[x]]==1 && !vis[a[x]])answer.push_back(a[x]),vis[a[x]]=1;
 //   op(a[x]);
}

inline void Read(int &x)
{
    char c=getchar();
    x=0;
    while (c<'0'||c>'9')
    {
        c=getchar();
    }
    while (c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
}
inline void Out(int a)    //输出一个整型
{
    if(a<0)
    {
          putchar('-');
          a=-a;
    }
    if(a>9)
    Out(a/10);
    putchar(a%10+'0');
}

int main()
{
//    freopen("input.txt","r",stdin);
  //  freopen("output.txt","w",stdout);
    n=nextInt();
    b=sqrt(n);
    for(int i=1; i<=n; i++)
    {
        a[i]=nextInt();
    }
    m=nextInt();
    for(int i=1; i<=m; i++)
    {
        que[i].l=nextInt();
        que[i].r=nextInt();
        que[i].id=i;
    }
    sort(que+1,que+m+1,cmp);
    int l=1,r=0;
    c[0]=1;answer.push_back(0);
    for(int i=1; i<=m; i++)
    {
        while(l>que[i].l)   add(--l);
        while(l<que[i].l)   del(l++);
        while(r<que[i].r)   add(++r);
        while(r>que[i].r)   del(r--);
        while (c[answer.back()]!=1)
        {
            vis[answer.back()]=0;
            answer.pop_back();
        }
        ans[que[i].id]=answer.back();
    }
    for(int i=1; i<=m; i++)
    {
        putInt(ans[i]);
        putChar('\n');
    }
    flushInt();
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
using namespace std;
const int N = 500500;
struct node
{
    int l,r,id;
}   que[N];
int a[N],c[N];
int vis[N];
int n,m;
int b;
int ans[N];

inline int cmp(const node &x,const node &y)    const
{
    if (x.l/b<y.l/b)
        return 1;
    if (x.l/b>y.l/b)
        return 0;
    return ((x.l/b)&1)?x.r<y.r:x.r>y.r;
}

vector<int> answer;

inline void op(int x)
{
	if(c[x]==1 && !vis[x])answer.push_back(x),vis[x]=1;
}

inline void add(int x)
{
    c[a[x]]++;
    op(a[x]);
}
inline void del(int x)
{
    c[a[x]]--;
    op(a[x]);
}

inline void Read(int &x)
{
    char c=getchar();
  //  int f=1;
    x=0;
    while (c<'0'||c>'9')
    {
   //     if (c=='-')
 //           f=-f;
        c=getchar();
    }
    while (c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
  //  x*=f;
}
inline void Out(int a)    //输出一个整型
{
    if(a<0)
    {
          putchar('-');
          a=-a;
    }
    if(a>9)
    Out(a/10);
    putchar(a%10+'0');
}

int main()
{
   // freopen("input.txt","r",stdin);
    Read(n);
    b=sqrt(n);
    for(int i=1; i<=n; i++)
    {
        Read(a[i]);
       // scanf("%d",&a[i]);
    }
    Read(m);
    for(int i=1; i<=m; i++)
    {
        Read(que[i].l);
        Read(que[i].r);
        que[i].id=i;
    }
    sort(que+1,que+m+1,cmp);
    int l=1,r=0;
    c[0]=1;answer.push_back(0);
    for(int i=1; i<=m; i++)
    {
        while(l>que[i].l)   add(--l);
        while(l<que[i].l)   del(l++);
        while(r<que[i].r)   add(++r);
        while(r>que[i].r)   del(r--);
        while (c[answer.back()]!=1)
        {
            vis[answer.back()]=0;
            answer.pop_back();
        }
        ans[que[i].id]=answer.back();
    }
    for(int i=1; i<=m; i++)
    {
        Out(ans[i]);
        putchar('\n');
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
using namespace std;
const int N = 500500;
struct node
{
    int l,r,id;
}   que[N];
int a[N],c[N];
bool vis[N];
int n,m;
int b;
int ans[N];

inline bool cmp(const node &x,const node &y)
{
    if (x.l/b<y.l/b)
        return 1;
    if (x.l/b>y.l/b)
        return 0;
    return ((x.l/b)&1)?x.r<y.r:x.r>y.r;
}

vector<int> answer;

inline void op(int x)
{
	if(c[x]==1 && !vis[x])answer.push_back(x),vis[x]=1;
}

inline void add(int x)
{
    c[a[x]]++;
    op(a[x]);
}
inline void del(int x)
{
    c[a[x]]--;
    op(a[x]);
}

inline void Read(int &x)
{
    char c=getchar();
  //  int f=1;
    x=0;
    while (c<'0'||c>'9')
    {
   //     if (c=='-')
 //           f=-f;
        c=getchar();
    }
    while (c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
  //  x*=f;
}
inline void Out(int a)    //输出一个整型
{
    if(a<0)
    {
          putchar('-');
          a=-a;
    }
    if(a>9)
    Out(a/10);
    putchar(a%10+'0');
}

int main()
{
   // freopen("input.txt","r",stdin);
    Read(n);
    b=sqrt(n);
    for(int i=1; i<=n; i++)
    {
        Read(a[i]);
       // scanf("%d",&a[i]);
    }
    Read(m);
    for(int i=1; i<=m; i++)
    {
        Read(que[i].l);
        Read(que[i].r);
        que[i].id=i;
    }
    sort(que+1,que+m+1,cmp);
    int l=1,r=0;
    c[0]=1;answer.push_back(0);
    for(int i=1; i<=m; i++)
    {
        while(l>que[i].l)   add(--l);
        while(l<que[i].l)   del(l++);
        while(r<que[i].r)   add(++r);
        while(r>que[i].r)   del(r--);
        while (c[answer.back()]!=1)
        {
            vis[answer.back()]=0;
            answer.pop_back();
        }
        ans[que[i].id]=answer.back();
    }
    for(int i=1; i<=m; i++)
    {
        Out(ans[i]);
        putchar('\n');
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>

char _buf[1 << 16];
int _pos = 1;
int _cap = 1;

char _obuf[1 << 16];
int _opos = 0;

unsigned char* _abuf;
int _apos = 1 << 20;

inline char nextChar()
{
	if (_cap == 0) return 0;
	if (_pos == _cap)
	{
		_cap = (int)fread(_buf, 1, 1 << 16, stdin);
		if (_cap == 0) return 0;
		_pos = 0;
	}
	return _buf[_pos++];
}

inline int nextInt()
{
	char c = nextChar();
	while (isspace(c)) c = nextChar();
	int res = 0;
	while (isdigit(c))
	{
		res = res * 10 + c - '0';
		c = nextChar();
	}
	return res;
}

inline void putInt(int x)
{
	if (x < 0)
	{
		_obuf[_opos++] = '-';
		x = -x;
	}
	int cur = _opos;
	while (x >= 10)
	{
		_obuf[_opos++] = (char)(x % 10 + '0');
		x /= 10;
	}
	_obuf[_opos++] = (char)(x + '0');
	for (int i = cur, j = _opos - 1; i < j; ++i, --j)
	{
		_obuf[i] ^= _obuf[j];
		_obuf[j] ^= _obuf[i];
		_obuf[i] ^= _obuf[j];
	}
	_obuf[_opos++] = ' ';
	if (_opos > 64000)
	{
		fwrite(_obuf, 1, _opos, stdout);
		_opos = 0;
	}
}

inline void flushInt()
{
	if (_opos)  fwrite(_obuf, 1, _opos, stdout);
	fflush(stdout);
}

using namespace std;
const int N = 500500;
struct node
{
    int l,r,id;
}   que[N];
int a[N],c[N];
bool vis[N];
int n,m;
int b;
int ans[N];

inline bool cmp(const node &x,const node &y)
{
    if (x.l/b<y.l/b)
        return 1;
    if (x.l/b>y.l/b)
        return 0;
    return ((x.l/b)&1)?x.r<y.r:x.r>y.r;
}

vector<int> answer;

inline void op(int x)
{
	if(c[x]==1 && !vis[x])answer.push_back(x),vis[x]=1;
}

inline void add(int x)
{
    c[a[x]]++;
    op(a[x]);
}
inline void del(int x)
{
    c[a[x]]--;
    op(a[x]);
}

inline void Read(int &x)
{
    char c=getchar();
    x=0;
    while (c<'0'||c>'9')
    {
        c=getchar();
    }
    while (c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
}
inline void Out(int a)    //输出一个整型
{
    if(a<0)
    {
          putchar('-');
          a=-a;
    }
    if(a>9)
    Out(a/10);
    putchar(a%10+'0');
}

int main()
{
    n=nextInt();
    b=sqrt(n);
    for(int i=1; i<=n; i++)
    {
        a[i]=nextInt();
    }
    m=nextInt();
    for(int i=1; i<=m; i++)
    {
        que[i].l=nextInt();
        que[i].r=nextInt();
        que[i].id=i;
    }
    sort(que+1,que+m+1,cmp);
    int l=1,r=0;
    c[0]=1;answer.push_back(0);
    for(int i=1; i<=m; i++)
    {
        while(l>que[i].l)   add(--l);
        while(l<que[i].l)   del(l++);
        while(r<que[i].r)   add(++r);
        while(r>que[i].r)   del(r--);
        while (c[answer.back()]!=1)
        {
            vis[answer.back()]=0;
            answer.pop_back();
        }
        ans[que[i].id]=answer.back();
    }
    for(int i=1; i<=m; i++)
    {
        putInt(ans[i]);
    }
    flushInt();
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
using namespace std;
const int N = 500500;
struct node
{
    int l,r,id,ans;
}   q[N];
int a[N],c[N];
bool vis[N];
int n,m;
int b;

inline bool cmp(const node &x,const node &y)
{
    if (x.l/b<y.l/b)
        return 1;
    if (x.l/b>y.l/b)
        return 0;
    return ((x.l/b)&1)?x.r<y.r:x.r>y.r;
}
vector<int> answer;

inline int cmp2(const node &x,const node &y)
{
	return x.id<y.id;
}

inline void op(int x)
{
	if(c[x]==1 && !vis[x])answer.push_back(x),vis[x]=1;
}

inline void add(int x)
{
    c[a[x]]++;
    op(a[x]);
}
inline void del(int x)
{
    c[a[x]]--;
    op(a[x]);
}

inline void Read(int &x)
{
    char c=getchar();
  //  int f=1;
    x=0;
    while (c<'0'||c>'9')
    {
   //     if (c=='-')
 //           f=-f;
        c=getchar();
    }
    while (c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
  //  x*=f;
}
inline void Out(int a)    //输出一个整型
{
    if(a<0)
    {
          putchar('-');
          a=-a;
    }
    if(a>9)
    Out(a/10);
    putchar(a%10+'0');
}

int main()
{
   // freopen("input.txt","r",stdin);
    Read(n);
    b=sqrt(n);
    for(int i=1; i<=n; i++)
    {
        Read(a[i]);
       // scanf("%d",&a[i]);
    }
    Read(m);
    for(int i=1; i<=m; i++)
    {
        Read(q[i].l);
        Read(q[i].r);
        q[i].id=i;
      //  scanf("%d%d",&q[i].l,&q[i].r),q[i].id=i;
    }
    sort(q+1,q+m+1,cmp);
    int l=1,r=0;
    c[0]=1;answer.push_back(0);
    for(int i=1; i<=m; i++)
    {
        while(l>q[i].l)
            add(--l);
        while(l<q[i].l)
            del(l++);
        while(r<q[i].r)
            add(++r);
        while(r>q[i].r)
            del(r--);
        while (c[answer.back()]!=1)
        {
            vis[answer.back()]=0;
            answer.pop_back();
        }
        q[i].ans=answer.back();
    }
    sort(q+1,q+m+1,cmp2);
    for(int i=1; i<=m; i++)
    {
        Out(q[i].ans);
        putchar('\n');
    }
     //   printf("%d\n",ans[i]);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
using namespace std;
const int N = 500500;
struct node
{
    int l,r,id,ans;
}   que[N];
int a[N],c[N];
bool vis[N];
int n,m;
int b;
int ans[N];

inline bool cmp(const node &x,const node &y)
{
    if (x.l/b<y.l/b)
        return 1;
    if (x.l/b>y.l/b)
        return 0;
    return ((x.l/b)&1)?x.r<y.r:x.r>y.r;
}
vector<int> answer;

inline int cmp2(const node &x,const node &y)
{
	return x.id<y.id;
}

inline void op(int x)
{
	if(c[x]==1 && !vis[x])answer.push_back(x),vis[x]=1;
}

inline void add(int x)
{
    c[a[x]]++;
    op(a[x]);
}
inline void del(int x)
{
    c[a[x]]--;
    op(a[x]);
}

inline void Read(int &x)
{
    char c=getchar();
  //  int f=1;
    x=0;
    while (c<'0'||c>'9')
    {
   //     if (c=='-')
 //           f=-f;
        c=getchar();
    }
    while (c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
  //  x*=f;
}
inline void Out(int a)    //输出一个整型
{
    if(a<0)
    {
          putchar('-');
          a=-a;
    }
    if(a>9)
    Out(a/10);
    putchar(a%10+'0');
}

int main()
{
   // freopen("input.txt","r",stdin);
    Read(n);
    b=sqrt(n);
    for(int i=1; i<=n; i++)
    {
        Read(a[i]);
       // scanf("%d",&a[i]);
    }
    Read(m);
    for(int i=1; i<=m; i++)
    {
        Read(que[i].l);
        Read(que[i].r);
        que[i].id=i;
      //  scanf("%d%d",&q[i].l,&q[i].r),q[i].id=i;
    }
    sort(que+1,que+m+1,cmp);
    int l=1,r=0;
    c[0]=1;answer.push_back(0);
    for(int i=1; i<=m; i++)
    {
        while(l>que[i].l)   add(--l);
        while(l<que[i].l)   del(l++);
        while(r<que[i].r)   add(++r);
        while(r>que[i].r)   del(r--);
        while (c[answer.back()]!=1)
        {
            vis[answer.back()]=0;
            answer.pop_back();
        }
        ans[que[i].id]=answer.back();
    //    que[i].ans=answer.back();
    }
  //  sort(que+1,que+m+1,cmp2);
    for(int i=1; i<=m; i++)
    {
        Out(ans[i]);
      //  Out(que[i].ans);
        putchar('\n');
    }
     //   printf("%d\n",ans[i]);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
using namespace std;
const int N = 500500;
struct node
{
    int l,r,id;
}   que[N];
int a[N],c[N];
int vis[N];
int n,m;
int b;
int ans[N];

inline int cmp(const node &x,const node &y)
{
    if (x.l/b<y.l/b)
        return 1;
    if (x.l/b>y.l/b)
        return 0;
    return ((x.l/b)&1)?x.r<y.r:x.r>y.r;
}

vector<int> answer;

inline void op(int x)
{
	if(c[x]==1 && !vis[x])answer.push_back(x),vis[x]=1;
}

inline void add(int x)
{
    c[a[x]]++;
    op(a[x]);
}
inline void del(int x)
{
    c[a[x]]--;
    op(a[x]);
}

inline void Read(int &x)
{
    char c=getchar();
  //  int f=1;
    x=0;
    while (c<'0'||c>'9')
    {
   //     if (c=='-')
 //           f=-f;
        c=getchar();
    }
    while (c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
  //  x*=f;
}
inline void Out(int a)    //输出一个整型
{
    if(a<0)
    {
          putchar('-');
          a=-a;
    }
    if(a>9)
    Out(a/10);
    putchar(a%10+'0');
}

int main()
{
   // freopen("input.txt","r",stdin);
    Read(n);
    b=sqrt(n);
    for(int i=1; i<=n; i++)
    {
        Read(a[i]);
       // scanf("%d",&a[i]);
    }
    Read(m);
    for(int i=1; i<=m; i++)
    {
        Read(que[i].l);
        Read(que[i].r);
        que[i].id=i;
    }
    sort(que+1,que+m+1,cmp);
    int l=1,r=0;
    c[0]=1;answer.push_back(0);
    for(int i=1; i<=m; i++)
    {
        while(l>que[i].l)   add(--l);
        while(l<que[i].l)   del(l++);
        while(r<que[i].r)   add(++r);
        while(r>que[i].r)   del(r--);
        while (c[answer.back()]!=1)
        {
            vis[answer.back()]=0;
            answer.pop_back();
        }
        ans[que[i].id]=answer.back();
    }
    for(int i=1; i<=m; i++)
    {
        Out(ans[i]);
        putchar('\n');
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
using namespace std;
const int N = 500500;
struct node
{
    int l,r,id;
}   que[N];
int a[N],c[N];
bool vis[N];
int n,m;
int b;
int ans[N];

inline int cmp(const node &x,const node &y)
{
    if (x.l/b<y.l/b)
        return 1;
    if (x.l/b>y.l/b)
        return 0;
    return ((x.l/b)&1)?x.r<y.r:x.r>y.r;
}

vector<int> answer;

inline void op(int x)
{
	if(c[x]==1 && !vis[x])answer.push_back(x),vis[x]=1;
}

inline void add(int x)
{
    c[a[x]]++;
    op(a[x]);
}
inline void del(int x)
{
    c[a[x]]--;
    op(a[x]);
}

inline void Read(int &x)
{
    char c=getchar();
  //  int f=1;
    x=0;
    while (c<'0'||c>'9')
    {
   //     if (c=='-')
 //           f=-f;
        c=getchar();
    }
    while (c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
  //  x*=f;
}
inline void Out(int a)    //输出一个整型
{
    if(a<0)
    {
          putchar('-');
          a=-a;
    }
    if(a>9)
    Out(a/10);
    putchar(a%10+'0');
}

int main()
{
   // freopen("input.txt","r",stdin);
    Read(n);
    b=sqrt(n);
    for(int i=1; i<=n; i++)
    {
        Read(a[i]);
       // scanf("%d",&a[i]);
    }
    Read(m);
    for(int i=1; i<=m; i++)
    {
        Read(que[i].l);
        Read(que[i].r);
        que[i].id=i;
    }
    sort(que+1,que+m+1,cmp);
    int l=1,r=0;
    c[0]=1;answer.push_back(0);
    for(int i=1; i<=m; i++)
    {
        while(l>que[i].l)   add(--l);
        while(l<que[i].l)   del(l++);
        while(r<que[i].r)   add(++r);
        while(r>que[i].r)   del(r--);
        while (c[answer.back()]!=1)
        {
            vis[answer.back()]=0;
            answer.pop_back();
        }
        ans[que[i].id]=answer.back();
    }
    for(int i=1; i<=m; i++)
    {
        Out(ans[i]);
        putchar('\n');
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
using namespace std;
const int N = 500500;
struct node
{
    int l,r,id;
}   que[N];
int a[N],c[N];
char vis[N];
int n,m;
int b;
int ans[N];

inline char cmp(const node &x,const node &y)
{
    if (x.l/b<y.l/b)
        return 1;
    if (x.l/b>y.l/b)
        return 0;
    return ((x.l/b)&1)?x.r<y.r:x.r>y.r;
}

vector<int> answer;

inline void op(int x)
{
	if(c[x]==1 && !vis[x])answer.push_back(x),vis[x]=1;
}

inline void add(int x)
{
    c[a[x]]++;
    op(a[x]);
}
inline void del(int x)
{
    c[a[x]]--;
    op(a[x]);
}

inline void Read(int &x)
{
    char c=getchar();
  //  int f=1;
    x=0;
    while (c<'0'||c>'9')
    {
   //     if (c=='-')
 //           f=-f;
        c=getchar();
    }
    while (c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
  //  x*=f;
}
inline void Out(int a)    //输出一个整型
{
    if(a<0)
    {
          putchar('-');
          a=-a;
    }
    if(a>9)
    Out(a/10);
    putchar(a%10+'0');
}

int main()
{
   // freopen("input.txt","r",stdin);
    Read(n);
    b=sqrt(n);
    for(int i=1; i<=n; i++)
    {
        Read(a[i]);
       // scanf("%d",&a[i]);
    }
    Read(m);
    for(int i=1; i<=m; i++)
    {
        Read(que[i].l);
        Read(que[i].r);
        que[i].id=i;
    }
    sort(que+1,que+m+1,cmp);
    int l=1,r=0;
    c[0]=1;answer.push_back(0);
    for(int i=1; i<=m; i++)
    {
        while(l>que[i].l)   add(--l);
        while(l<que[i].l)   del(l++);
        while(r<que[i].r)   add(++r);
        while(r>que[i].r)   del(r--);
        while (c[answer.back()]!=1)
        {
            vis[answer.back()]=0;
            answer.pop_back();
        }
        ans[que[i].id]=answer.back();
    }
    for(int i=1; i<=m; i++)
    {
        Out(ans[i]);
        putchar('\n');
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>

char _buf[1 << 16];
int _pos = 1;
int _cap = 1;

char _obuf[1 << 16];
int _opos = 0;

unsigned char* _abuf;
int _apos = 1 << 20;

inline char nextChar()
{
	if (_cap == 0) return 0;
	if (_pos == _cap)
	{
		_cap = (int)fread(_buf, 1, 1 << 16, stdin);
		if (_cap == 0) return 0;
		_pos = 0;
	}
	return _buf[_pos++];
}

template<typename  T>
inline void nextInt(T &res)
{
	char c = nextChar();
	while (isspace(c)) c = nextChar();
	res = 0;
	while (isdigit(c))
	{
		res = res * 10 + c - '0';
		c = nextChar();
	}
	//return res;
}
char tmp;
inline void putInt(int x)
{
	if (x < 0)
	{
		_obuf[_opos++] = '-';
		x = -x;
	}
	int cur = _opos;
	while (x >= 10)
	{
		_obuf[_opos++] = (char)(x % 10 + '0');
		x /= 10;
	}
	_obuf[_opos++] = (char)(x + '0');
	for (int i = cur, j = _opos - 1; i < j; ++i, --j)
	{
	    tmp=_obuf[i];
	    _obuf[i]=_obuf[j];
	    _obuf[j]=tmp;
	}

	if (_opos > 64000)
	{
		fwrite(_obuf, 1, _opos, stdout);
		_opos = 0;
	}
}
inline void putChar(char x)
{
	_obuf[_opos++] = x;
	if (_opos > 64000)
	{
		fwrite(_obuf, 1, _opos, stdout);
		_opos = 0;
	}
}

inline void flushInt()
{
	if (_opos)  fwrite(_obuf, 1, _opos, stdout);
	fflush(stdout);
}

using namespace std;
const int N = 500500;
struct node
{
    int l,r,id;
}   que[N];
int a[N],c[N];
bool vis[N];
int n,m;
int b;
int ans[N];

inline bool cmp(const node &x,const node &y)
{
    if (x.l/b<y.l/b)
        return 1;
    if (x.l/b>y.l/b)
        return 0;
    return ((x.l/b)&1)?x.r<y.r:x.r>y.r;
}

vector<int> answer;

inline void op(int x)
{
	if(c[x]==1 && !vis[x])answer.push_back(x),vis[x]=1;
}

inline void add(int x)
{
    c[a[x]]++;
    op(a[x]);
}
inline void del(int x)
{
    c[a[x]]--;
    op(a[x]);
}

int main()
{
  //  freopen("input.txt","r",stdin);
   // freopen("output.txt","w",stdout);
    nextInt(n);
    b=sqrt(n);
    for(int i=1; i<=n; i++)
    {
        nextInt(a[i]);
    }
    nextInt(m);
    for(int i=1; i<=m; i++)
    {
        nextInt(que[i].l);
        nextInt(que[i].r);
        que[i].id=i;
    }
    sort(que+1,que+m+1,cmp);
    int l=1,r=0;
    c[0]=1;answer.push_back(0);
    for(int i=1; i<=m; i++)
    {
        while(l>que[i].l)   add(--l);
        while(l<que[i].l)   del(l++);
        while(r<que[i].r)   add(++r);
        while(r>que[i].r)   del(r--);
        while (c[answer.back()]!=1)
        {
            vis[answer.back()]=0;
            answer.pop_back();
        }
        ans[que[i].id]=answer.back();
    }
    for(int i=1; i<=m; i++)
    {
        putInt(ans[i]);
        putChar('\n');
    }
    flushInt();
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>

char _buf[1 << 16];
int _pos = 1;
int _cap = 1;

char _obuf[1 << 16];
int _opos = 0;

unsigned char* _abuf;
int _apos = 1 << 20;

inline char nextChar()
{
	if (_cap == 0) return 0;
	if (_pos == _cap)
	{
		_cap = (int)fread(_buf, 1, 1 << 16, stdin);
		if (_cap == 0) return 0;
		_pos = 0;
	}
	return _buf[_pos++];
}

inline int nextInt()
{
	char c = nextChar();
	while (isspace(c)) c = nextChar();
	int res = 0;
	while (isdigit(c))
	{
		res = res * 10 + c - '0';
		c = nextChar();
	}
	return res;
}

inline void putInt(int x)
{
	if (x < 0)
	{
		_obuf[_opos++] = '-';
		x = -x;
	}
	int cur = _opos;
	while (x >= 10)
	{
		_obuf[_opos++] = (char)(x % 10 + '0');
		x /= 10;
	}
	_obuf[_opos++] = (char)(x + '0');
	for (int i = cur, j = _opos - 1; i < j; ++i, --j)
	{
		_obuf[i] ^= _obuf[j];
		_obuf[j] ^= _obuf[i];
		_obuf[i] ^= _obuf[j];
	}
	//_obuf[_opos++] = ' ';
	if (_opos > 64000)
	{
		fwrite(_obuf, 1, _opos, stdout);
		_opos = 0;
	}
}
inline void putChar(char x)
{
	_obuf[_opos++] = x;
	if (_opos > 64000)
	{
		fwrite(_obuf, 1, _opos, stdout);
		_opos = 0;
	}
}

inline void flushInt()
{
	if (_opos)  fwrite(_obuf, 1, _opos, stdout);
	fflush(stdout);
}

using namespace std;
const int N = 500500;
struct node
{
    int l,r,id;
}   que[N];
int a[N],c[N];
bool vis[N];
int n,m;
int b;
int ans[N];

inline bool cmp(const node &x,const node &y)
{
    if (x.l/b<y.l/b)
        return 1;
    if (x.l/b>y.l/b)
        return 0;
    return ((x.l/b)&1)?x.r<y.r:x.r>y.r;
}

vector<int> answer;

inline void op(int x)
{
	if(c[x]==1 && !vis[x])answer.push_back(x),vis[x]=1;
}

inline void add(int x)
{
    c[a[x]]++;
    op(a[x]);
}
inline void del(int x)
{
    c[a[x]]--;
    op(a[x]);
}

inline void Read(int &x)
{
    char c=getchar();
    x=0;
    while (c<'0'||c>'9')
    {
        c=getchar();
    }
    while (c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
}
inline void Out(int a)    //输出一个整型
{
    if(a<0)
    {
          putchar('-');
          a=-a;
    }
    if(a>9)
    Out(a/10);
    putchar(a%10+'0');
}

int main()
{
//    freopen("input.txt","r",stdin);
  //  freopen("output.txt","w",stdout);
    n=nextInt();
    b=sqrt(n);
    for(int i=1; i<=n; i++)
    {
        a[i]=nextInt();
    }
    m=nextInt();
    for(int i=1; i<=m; i++)
    {
        que[i].l=nextInt();
        que[i].r=nextInt();
        que[i].id=i;
    }
    sort(que+1,que+m+1,cmp);
    int l=1,r=0;
    c[0]=1;answer.push_back(0);
    for(int i=1; i<=m; i++)
    {
        while(l>que[i].l)   add(--l);
        while(l<que[i].l)   del(l++);
        while(r<que[i].r)   add(++r);
        while(r>que[i].r)   del(r--);
        while (c[answer.back()]!=1)
        {
            vis[answer.back()]=0;
            answer.pop_back();
        }
        ans[que[i].id]=answer.back();
    }
    for(int i=1; i<=m; i++)
    {
        putInt(ans[i]);
        putChar('\n');
    }
    flushInt();
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>

char _buf[1 << 16];
int _pos = 1;
int _cap = 1;

char _obuf[1 << 16];
int _opos = 0;

unsigned char* _abuf;
int _apos = 1 << 20;

inline char nextChar()
{
	if (_cap == 0) return 0;
	if (_pos == _cap)
	{
		_cap = (int)fread(_buf, 1, 1 << 16, stdin);
		if (_cap == 0) return 0;
		_pos = 0;
	}
	return _buf[_pos++];
}

inline int nextInt()
{
	char c = nextChar();
	while (isspace(c)) c = nextChar();
	int res = 0;
	while (isdigit(c))
	{
		res = res * 10 + c - '0';
		c = nextChar();
	}
	return res;
}

char tmp;
inline void putInt(int x)
{
	if (x < 0)
	{
		_obuf[_opos++] = '-';
		x = -x;
	}
	int cur = _opos;
	while (x >= 10)
	{
		_obuf[_opos++] = (char)(x % 10 + '0');
		x /= 10;
	}
	_obuf[_opos++] = (char)(x + '0');
	for (int i = cur, j = _opos - 1; i < j; ++i, --j)
	{
	    tmp=_obuf[i];
	    _obuf[i]=_obuf[j];
	    _obuf[j]=tmp;
	//	_obuf[i] ^= _obuf[j];
	//	_obuf[j] ^= _obuf[i];
	//	_obuf[i] ^= _obuf[j];
	}
	//_obuf[_opos++] = ' ';
	if (_opos > 64000)
	{
		fwrite(_obuf, 1, _opos, stdout);
		_opos = 0;
	}
}
inline void putChar(char x)
{
	_obuf[_opos++] = x;
	if (_opos > 64000)
	{
		fwrite(_obuf, 1, _opos, stdout);
		_opos = 0;
	}
}

inline void flushInt()
{
	if (_opos)  fwrite(_obuf, 1, _opos, stdout);
	fflush(stdout);
}

using namespace std;
const int N = 500500;
struct node
{
    int l,r,id;
}   que[N];
int a[N],c[N];
bool vis[N];
int n,m;
int b;
int ans[N];

inline bool cmp(const node &x,const node &y)
{
    if (x.l/b<y.l/b)
        return 1;
    if (x.l/b>y.l/b)
        return 0;
    return ((x.l/b)&1)?x.r<y.r:x.r>y.r;
}

vector<int> answer;

inline void op(int x)
{
	if(c[x]==1 && !vis[x])answer.push_back(x),vis[x]=1;
}

inline void add(int x)
{
    c[a[x]]++;
    op(a[x]);
}
inline void del(int x)
{
    c[a[x]]--;
    op(a[x]);
}

inline void Read(int &x)
{
    char c=getchar();
    x=0;
    while (c<'0'||c>'9')
    {
        c=getchar();
    }
    while (c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
}
inline void Out(int a)    //输出一个整型
{
    if(a<0)
    {
          putchar('-');
          a=-a;
    }
    if(a>9)
    Out(a/10);
    putchar(a%10+'0');
}

int main()
{
//    freopen("input.txt","r",stdin);
  //  freopen("output.txt","w",stdout);
    n=nextInt();
    b=sqrt(n);
    for(int i=1; i<=n; i++)
    {
        a[i]=nextInt();
    }
    m=nextInt();
    for(int i=1; i<=m; i++)
    {
        que[i].l=nextInt();
        que[i].r=nextInt();
        que[i].id=i;
    }
    sort(que+1,que+m+1,cmp);
    int l=1,r=0;
    c[0]=1;answer.push_back(0);
    for(int i=1; i<=m; i++)
    {
        while(l>que[i].l)   add(--l);
        while(l<que[i].l)   del(l++);
        while(r<que[i].r)   add(++r);
        while(r>que[i].r)   del(r--);
        while (c[answer.back()]!=1)
        {
            vis[answer.back()]=0;
            answer.pop_back();
        }
        ans[que[i].id]=answer.back();
    }
    for(int i=1; i<=m; i++)
    {
        putInt(ans[i]);
        putChar('\n');
    }
    flushInt();
    return 0;
}


/*
  Task: F. One Occurrence
  Link: http://codeforces.com/contest/1000/problem/F
*/
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

void err(istream_iterator<string> it) { cerr << endl; }
template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << ", "; err(++it, args...); }
#define db(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define dba(ar) { cerr << #ar << " = ["; for (auto x : ar) cerr << " " << x; cerr << "]" << endl; }
typedef long long int64;
#define ret(x) { cout << x << endl; return; }

int n, nQueries, sqn;
vector<int> a;

struct Q {
  int l, r;
  int idx;
};
vector<Q> qs;

int cnt[500050];
bitset <500050> can;
int cd [500050];
int curAns = -1;

inline void inc(int v) {
  ++cnt[v];
  if (cnt[v] == 1 && !can[v]) {
    can[v] = true;
    cd[++curAns] = v;
  }
};
inline void dec(int v) {
  --cnt[v];
  if (cnt[v] == 1 && !can[v]) {
    can[v] = true;
    cd[++curAns] = v;
  }
};
inline int getAns() {
  while (curAns >= 0 && cnt[cd[curAns]] != 1) {
    can[cd[curAns]] = false;
    --curAns;
  }
  return (curAns >= 0 ? cd[curAns] : 0);
}

void solve() {
  cin >> n; sqn = max(1, (int) sqrt(n));
  a = decltype(a)(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  cin >> nQueries;
  qs = vector<Q>(nQueries);
  for (int i = 0; i < nQueries; ++i) {
    int l, r; cin >> l >> r; --l; --r;
    qs[i].l = l; qs[i].r = r; qs[i].idx = i;
  }
  sort(begin(qs), end(qs), [](const Q a, const Q b) {
    if (a.l / sqn != b.l / sqn) {
      return a.l < b.l;
    }
    return ((a.l / sqn) & 1) ? (a.r < b.r) : (a.r > b.r);
  });

  vector<int> ans(nQueries);
  memset(cnt, 0, sizeof(cnt));

  int l = 0, r = -1;
  for (int i = 0; i < nQueries; ++i) {
    auto q = qs[i];
      while (r < q.r) { ++r; inc(a[r]); }
      while (l > q.l) { --l; inc(a[l]); }
      while (r > q.r) { dec(a[r]); --r; }
      while (l < q.l) { dec(a[l]); ++l; }
      ans[q.idx] = getAns();
  }
  for (int i = 0; i < nQueries; ++i) cout << ans[i] << '\n';
}

int main(int argc, char* argv[]) {
  ios::sync_with_stdio(false); cin.tie(nullptr);
  solve();
  return 0;
}


/*
  Task: F. One Occurrence
  Link: http://codeforces.com/contest/1000/problem/F
*/
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

void err(istream_iterator<string> it) { cerr << endl; }
template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << ", "; err(++it, args...); }
#define db(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define dba(ar) { cerr << #ar << " = ["; for (auto x : ar) cerr << " " << x; cerr << "]" << endl; }
typedef long long int64;
#define ret(x) { cout << x << endl; return; }

int n, nQueries, sqn;
vector<int> a;

struct Q {
  int l, r;
  int idx;
};
vector<Q> qs;

int cnt[500050];
unordered_set<int> vs;

inline void inc(int v) {
  ++cnt[v];
  if (cnt[v] == 2) {
    vs.erase(v);
  } else if (cnt[v] == 1) {
    vs.insert(v);
  }
  // vs[cnt[v]].erase(v);
  // vs[++cnt[v]].insert(v);
};
inline void dec(int v) {
  --cnt[v];
  if (cnt[v] == 1) {
    vs.insert(v);
  } else if (cnt[v] == 0) {
    vs.erase(v);
  }
  // vs[cnt[v]].erase(v);
  // vs[--cnt[v]].insert(v);
};

void solve() {
  cin >> n; sqn = max(1, (int) sqrt(n));
  a = decltype(a)(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  cin >> nQueries;
  qs = vector<Q>(nQueries);
  for (int i = 0; i < nQueries; ++i) {
    int l, r; cin >> l >> r; --l; --r;
    qs[i].l = l; qs[i].r = r; qs[i].idx = i;
  }
  sort(begin(qs), end(qs), [](const Q a, const Q b) {
    if (a.l / sqn != b.l / sqn) {
      return a.l < b.l;
    }
    return ((a.l / sqn) & 1) ? (a.r < b.r) : (a.r > b.r);
  });

  vector<int> ans(nQueries);
  memset(cnt, 0, sizeof(cnt));

  int l = 0, r = -1;
  for (int i = 0; i < nQueries; ++i) {
    auto q = qs[i];
      while (r < q.r) { ++r; inc(a[r]); }
      while (l > q.l) { --l; inc(a[l]); }
      while (r > q.r) { dec(a[r]); --r; }
      while (l < q.l) { dec(a[l]); ++l; }
      if (vs.empty()) ans[q.idx] = 0;
      else ans[q.idx] = *vs.begin();
  }
  for (int i = 0; i < nQueries; ++i) cout << ans[i] << '\n';
}

int main(int argc, char* argv[]) {
  ios::sync_with_stdio(false); cin.tie(nullptr);
  solve();
  return 0;
}


/*
  Task: F. One Occurrence
  Link: http://codeforces.com/contest/1000/problem/F
*/
#include <bits/stdc++.h>
using namespace std;

void err(istream_iterator<string> it) { cerr << endl; }
template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << ", "; err(++it, args...); }
#define db(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define dba(ar) { cerr << #ar << " = ["; for (auto x : ar) cerr << " " << x; cerr << "]" << endl; }
typedef long long int64;
#define ret(x) { cout << x << endl; return; }

int n, nQueries, sqn;
vector<int> a;

struct Q {
  int l, r;
  int idx;
};
vector<Q> qs;

  int cnt[500050];
  vector<unordered_set<int>> vs(2);

  inline void inc(int v) {
    ++cnt[v];
    if (cnt[v] == 2) {
      vs[1].erase(v);
    } else if (cnt[v] == 1) {
      vs[1].insert(v);
    }
    // vs[cnt[v]].erase(v);
    // vs[++cnt[v]].insert(v);
  };
  inline void dec(int v) {
    --cnt[v];
    if (cnt[v] == 1) {
      vs[1].insert(v);
    } else if (cnt[v] == 0) {
      vs[1].erase(v);
    }
    // vs[cnt[v]].erase(v);
    // vs[--cnt[v]].insert(v);
  };

void solve() {
  cin >> n; sqn = max(1, (int) sqrt(n) + rand() % 100);
  a = decltype(a)(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  cin >> nQueries;
  qs = vector<Q>(nQueries);
  for (int i = 0; i < nQueries; ++i) {
    int l, r; cin >> l >> r; --l; --r;
    qs[i].l = l; qs[i].r = r; qs[i].idx = i;
  }
  sort(begin(qs), end(qs), [](const Q a, const Q b) {
    if (a.l / sqn != b.l / sqn) {
      return a.l < b.l;
    }
    return ((a.l / sqn) & 1) ? (a.r < b.r) : (a.r > b.r);
  });

  vector<int> ans(nQueries);
  memset(cnt, 0, sizeof(cnt));

  int l = 0, r = -1;
  for (int i = 0; i < nQueries; ++i) {
    auto q = qs[i];
      while (r < q.r) { ++r; inc(a[r]); }
      while (l > q.l) { --l; inc(a[l]); }
      while (r > q.r) { dec(a[r]); --r; }
      while (l < q.l) { dec(a[l]); ++l; }
      if (vs[1].empty()) ans[q.idx] = 0;
      else ans[q.idx] = *vs[1].begin();
  }
  for (int i = 0; i < nQueries; ++i) cout << ans[i] << '\n';
}

int main(int argc, char* argv[]) {
  ios::sync_with_stdio(false); cin.tie(nullptr);
  solve();
  return 0;
}


/*
  Task: F. One Occurrence
  Link: http://codeforces.com/contest/1000/problem/F
*/
#include <bits/stdc++.h>
using namespace std;

void err(istream_iterator<string> it) { cerr << endl; }
template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << ", "; err(++it, args...); }
#define db(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define dba(ar) { cerr << #ar << " = ["; for (auto x : ar) cerr << " " << x; cerr << "]" << endl; }
typedef long long int64;
#define ret(x) { cout << x << endl; return; }

int n, nQueries, sqn;
vector<int> a;

struct Q {
  int l, r;
  int idx;
};
vector<Q> qs;

void solve() {
  cin >> n; sqn = max(1, (int) sqrt(n));
  a = decltype(a)(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  cin >> nQueries;
  qs = decltype(qs)(nQueries);
  for (int i = 0; i < nQueries; ++i) {
    int l, r; cin >> l >> r; --l; --r;
    qs[i].l = l; qs[i].r = r; qs[i].idx = i;
  }
  sort(begin(qs), end(qs), [](Q a, Q b) {
    if (a.l / sqn != b.l / sqn) {
      return a.l - b.l;
    }
    return a.r - b.r;
  });

  map<int, int> cnt;
  vector<set<int>> vs(n + 1);
  vector<int> ans(nQueries);

  auto inc = [&](int v) {
    vs[cnt[v]].erase(v);
    ++cnt[v];
    vs[cnt[v]].insert(v);
  };
  auto dec = [&](int v) {
    // db(v, cnt[v]);
    if (cnt[v] <= 0) return;
    vs[cnt[v]].erase(v);
    --cnt[v];
    vs[cnt[v]].insert(v);
  };

  int l = 0, r = -1;
  for (int i = 0; i < nQueries; ++i) {
    auto q = qs[i];
    while (r < q.r) { ++r; inc(a[r]); }
    while (l > q.l) { --l; inc(a[l]); }
    while (r > q.r) { dec(a[r]); --r; }
    while (l < q.l) { dec(a[l]); ++l; }
    if (vs[1].empty()) ans[q.idx] = 0;
    else ans[q.idx] = *vs[1].begin();
  }
  for (int i = 0; i < nQueries; ++i) cout << ans[i] << endl;
}

int main(int argc, char* argv[]) {
  ios::sync_with_stdio(false); cin.tie(nullptr);
  solve();
  return 0;
}


/*
  Task: F. One Occurrence
  Link: http://codeforces.com/contest/1000/problem/F
*/
#include <bits/stdc++.h>
using namespace std;

void err(istream_iterator<string> it) { cerr << endl; }
template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << ", "; err(++it, args...); }
#define db(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define dba(ar) { cerr << #ar << " = ["; for (auto x : ar) cerr << " " << x; cerr << "]" << endl; }
typedef long long int64;
#define ret(x) { cout << x << endl; return; }

int n, nQueries, sqn;
vector<int> a;

struct Q {
  int l, r;
  int idx;
};
vector<Q> qs;

  map<int, int> cnt;
  vector<unordered_set<int>> vs(2);

  inline void inc(int v) {
    ++cnt[v];
    if (cnt[v] == 2) {
      vs[1].erase(v);
    } else if (cnt[v] == 1) {
      vs[1].insert(v);
    }
    // vs[cnt[v]].erase(v);
    // vs[++cnt[v]].insert(v);
  };
  inline void dec(int v) {
    --cnt[v];
    if (cnt[v] == 1) {
      vs[1].insert(v);
    } else if (cnt[v] == 0) {
      vs[1].erase(v);
    }
    // vs[cnt[v]].erase(v);
    // vs[--cnt[v]].insert(v);
  };

void solve() {
  cin >> n; sqn = max(1, (int) sqrt(n));
  a = decltype(a)(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  cin >> nQueries;
  qs = vector<Q>(nQueries);
  for (int i = 0; i < nQueries; ++i) {
    int l, r; cin >> l >> r; --l; --r;
    qs[i].l = l; qs[i].r = r; qs[i].idx = i;
  }
  sort(begin(qs), end(qs), [](const Q a, const Q b) {
    if (a.l / sqn != b.l / sqn) {
      return a.l < b.l;
    }
    return a.r < b.r;
  });

  vector<int> ans(nQueries);

  int l = 0, r = -1;
  for (int i = 0; i < nQueries; ++i) {
    auto q = qs[i];
      while (r < q.r) { ++r; inc(a[r]); }
      while (l > q.l) { --l; inc(a[l]); }
      while (r > q.r) { dec(a[r]); --r; }
      while (l < q.l) { dec(a[l]); ++l; }
      if (vs[1].empty()) ans[q.idx] = 0;
      else ans[q.idx] = *vs[1].begin();
  }
  for (int i = 0; i < nQueries; ++i) cout << ans[i] << '\n';
}

int main(int argc, char* argv[]) {
  ios::sync_with_stdio(false); cin.tie(nullptr);
  solve();
  return 0;
}


/*
  Task: F. One Occurrence
  Link: http://codeforces.com/contest/1000/problem/F
*/

#include <bits/stdc++.h>
using namespace std;

void err(istream_iterator<string> it) { cerr << endl; }
template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << ", "; err(++it, args...); }
#define db(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define dba(ar) { cerr << #ar << " = ["; for (auto x : ar) cerr << " " << x; cerr << "]" << endl; }
typedef long long int64;
#define ret(x) { cout << x << endl; return; }

int n, nQueries, sqn;
vector<int> a;

struct Q {
  int l, r;
  int idx;
};
vector<Q> qs;

int cnt[500050];
bitset <500050> can;
int cd [500050];
int curAns = -1;

inline void inc(int v) {
  ++cnt[v];
  if (cnt[v] == 1 && !can[v]) {
    can[v] = true;
    cd[++curAns] = v;
  }
};
inline void dec(int v) {
  --cnt[v];
  if (cnt[v] == 1 && !can[v]) {
    can[v] = true;
    cd[++curAns] = v;
  }
};
inline int getAns() {
  while (curAns >= 0 && cnt[cd[curAns]] != 1) {
    can[cd[curAns]] = false;
    --curAns;
  }
  return (curAns >= 0 ? cd[curAns] : 0);
}

void solve() {
  cin >> n; sqn = max(1, (int) sqrt(n));
  a = decltype(a)(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  cin >> nQueries;
  qs = vector<Q>(nQueries);
  for (int i = 0; i < nQueries; ++i) {
    int l, r; cin >> l >> r; --l; --r;
    qs[i].l = l; qs[i].r = r; qs[i].idx = i;
  }
  sort(begin(qs), end(qs), [](const Q a, const Q b) {
    if (a.l / sqn != b.l / sqn) {
      return a.l < b.l;
    }
    return ((a.l / sqn) & 1) ? (a.r < b.r) : (a.r > b.r);
  });

  vector<int> ans(nQueries);
  memset(cnt, 0, sizeof(cnt));

  int l = 0, r = -1;
  for (int i = 0; i < nQueries; ++i) {
    auto q = qs[i];
      while (r < q.r) { ++r; inc(a[r]); }
      while (l > q.l) { --l; inc(a[l]); }
      while (r > q.r) { dec(a[r]); --r; }
      while (l < q.l) { dec(a[l]); ++l; }
      ans[q.idx] = getAns();
  }
  for (int i = 0; i < nQueries; ++i) cout << ans[i] << '\n';
}

int main(int argc, char* argv[]) {
  ios::sync_with_stdio(false); cin.tie(nullptr);
  solve();
  return 0;
}


/*
  Task: F. One Occurrence
  Link: http://codeforces.com/contest/1000/problem/F
*/
#include <bits/stdc++.h>
using namespace std;

void err(istream_iterator<string> it) { cerr << endl; }
template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << ", "; err(++it, args...); }
#define db(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define dba(ar) { cerr << #ar << " = ["; for (auto x : ar) cerr << " " << x; cerr << "]" << endl; }
typedef long long int64;
#define ret(x) { cout << x << endl; return; }

int n, nQueries, sqn;
vector<int> a;

struct Q {
  int l, r;
  int idx;
};
vector<Q> qs;

  int cnt[500050];
  vector<unordered_set<int>> vs(2);

  inline void inc(int v) {
    ++cnt[v];
    if (cnt[v] == 2) {
      vs[1].erase(v);
    } else if (cnt[v] == 1) {
      vs[1].insert(v);
    }
    // vs[cnt[v]].erase(v);
    // vs[++cnt[v]].insert(v);
  };
  inline void dec(int v) {
    --cnt[v];
    if (cnt[v] == 1) {
      vs[1].insert(v);
    } else if (cnt[v] == 0) {
      vs[1].erase(v);
    }
    // vs[cnt[v]].erase(v);
    // vs[--cnt[v]].insert(v);
  };

void solve() {
  cin >> n; sqn = max(1, (int) sqrt(n));
  a = decltype(a)(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  cin >> nQueries;
  qs = vector<Q>(nQueries);
  for (int i = 0; i < nQueries; ++i) {
    int l, r; cin >> l >> r; --l; --r;
    qs[i].l = l; qs[i].r = r; qs[i].idx = i;
  }
  sort(begin(qs), end(qs), [](const Q a, const Q b) {
    if (a.l / sqn != b.l / sqn) {
      return a.l < b.l;
    }
    return ((a.l / sqn) & 1) ? (a.r < b.r) : (a.r > b.r);
  });

  vector<int> ans(nQueries);
  memset(cnt, 0, sizeof(cnt));

  int l = 0, r = -1;
  for (int i = 0; i < nQueries; ++i) {
    auto q = qs[i];
      while (r < q.r) { ++r; inc(a[r]); }
      while (l > q.l) { --l; inc(a[l]); }
      while (r > q.r) { dec(a[r]); --r; }
      while (l < q.l) { dec(a[l]); ++l; }
      if (vs[1].empty()) ans[q.idx] = 0;
      else ans[q.idx] = *vs[1].begin();
  }
  for (int i = 0; i < nQueries; ++i) cout << ans[i] << '\n';
}

int main(int argc, char* argv[]) {
  ios::sync_with_stdio(false); cin.tie(nullptr);
  solve();
  return 0;
}


/*
  Task: F. One Occurrence
  Link: http://codeforces.com/contest/1000/problem/F
*/
#include <bits/stdc++.h>
using namespace std;

void err(istream_iterator<string> it) { cerr << endl; }
template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << ", "; err(++it, args...); }
#define db(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define dba(ar) { cerr << #ar << " = ["; for (auto x : ar) cerr << " " << x; cerr << "]" << endl; }
typedef long long int64;
#define ret(x) { cout << x << endl; return; }

int n, nQueries, sqn;
vector<int> a;

struct Q {
  int l, r;
  int idx;
};
vector<Q> qs;

void solve() {
  cin >> n; sqn = max(1, (int) sqrt(n));
  a = decltype(a)(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  cin >> nQueries;
  qs = decltype(qs)(nQueries);
  for (int i = 0; i < nQueries; ++i) {
    int l, r; cin >> l >> r; --l; --r;
    qs[i].l = l; qs[i].r = r; qs[i].idx = i;
  }
  sort(begin(qs), end(qs), [](Q a, Q b) {
    if (a.l / sqn != b.l / sqn) {
      return a.l - b.l;
    }
    return a.r - b.r;
  });

  int l = 0, r = -1;
  map<int, int> cnt;
  vector<set<int>> vs(n + 1);
  vector<int> ans(nQueries);

  auto inc = [&](int v) {
    vs[cnt[v]].erase(v);
    ++cnt[v];
    vs[cnt[v]].insert(v);
  };
  auto dec = [&](int v) {
    // db(v, cnt[v]);
    vs[cnt[v]].erase(v);
    --cnt[v];
    vs[cnt[v]].insert(v);
  };
  for (int i = 0; i < nQueries; ++i) {
    auto q = qs[i];
    while (r < q.r) { ++r; inc(a[r]);}
    while (l > q.l) { --l; inc(a[l]); }
    while (r > q.r) { dec(a[r]); --r; }
    while (l < q.l) { dec(a[l]); ++l; }
    if (vs[1].empty()) ans[q.idx] = 0;
    else ans[q.idx] = *vs[1].begin();
  }
  for (int i = 0; i < nQueries; ++i) cout << ans[i] << endl;
}

int main(int argc, char* argv[]) {
  ios::sync_with_stdio(false); cin.tie(nullptr);
  solve();
  return 0;
}


/*
  Task: F. One Occurrence
  Link: http://codeforces.com/contest/1000/problem/F
*/
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

void err(istream_iterator<string> it) { cerr << endl; }
template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << ", "; err(++it, args...); }
#define db(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define dba(ar) { cerr << #ar << " = ["; for (auto x : ar) cerr << " " << x; cerr << "]" << endl; }
typedef long long int64;
#define ret(x) { cout << x << endl; return; }

int n, nQueries, sqn;
vector<int> a;

struct Q {
  int l, r;
  int idx;
};
vector<Q> qs;

int cnt[500050];
bitset<500050> can;
int cd[500050];
int curAns = -1;

inline void inc(int v) {
  ++cnt[v];
  if (cnt[v] == 2) {
    can[v] = false;
    // if (cd[curAns] == v) --curAns;
  } else if (cnt[v] == 1) {
    can[v] = true;
    cd[++curAns] = v;
  }
};
inline void dec(int v) {
  --cnt[v];
  if (cnt[v] == 1) {
    can[v] = true;
    cd[++curAns] = v;
  } else if (cnt[v] == 0) {
    can[v] = false;
    // if (cd[curAns] == v) --curAns;
  }
};
inline int getAns() {
  while (curAns >= 0 && cnt[cd[curAns]] != 1) --curAns;
  return (curAns >= 0 ? cd[curAns] : 0);
}

void solve() {
  cin >> n; sqn = max(1, (int) sqrt(n));
  a = decltype(a)(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  cin >> nQueries;
  qs = vector<Q>(nQueries);
  for (int i = 0; i < nQueries; ++i) {
    int l, r; cin >> l >> r; --l; --r;
    qs[i].l = l; qs[i].r = r; qs[i].idx = i;
  }
  sort(begin(qs), end(qs), [](const Q a, const Q b) {
    if (a.l / sqn != b.l / sqn) {
      return a.l < b.l;
    }
    return ((a.l / sqn) & 1) ? (a.r < b.r) : (a.r > b.r);
  });

  vector<int> ans(nQueries);
  memset(cnt, 0, sizeof(cnt));

  int l = 0, r = -1;
  for (int i = 0; i < nQueries; ++i) {
    auto q = qs[i];
      while (r < q.r) { ++r; inc(a[r]); }
      while (l > q.l) { --l; inc(a[l]); }
      while (r > q.r) { dec(a[r]); --r; }
      while (l < q.l) { dec(a[l]); ++l; }
      ans[q.idx] = getAns();
  }
  for (int i = 0; i < nQueries; ++i) cout << ans[i] << '\n';
}

int main(int argc, char* argv[]) {
  ios::sync_with_stdio(false); cin.tie(nullptr);
  solve();
  return 0;
}


/*
  Task: F. One Occurrence
  Link: http://codeforces.com/contest/1000/problem/F
*/
#include <bits/stdc++.h>
using namespace std;

void err(istream_iterator<string> it) { cerr << endl; }
template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << ", "; err(++it, args...); }
#define db(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define dba(ar) { cerr << #ar << " = ["; for (auto x : ar) cerr << " " << x; cerr << "]" << endl; }
typedef long long int64;
#define ret(x) { cout << x << endl; return; }

int n, nQueries, sqn;
vector<int> a;

struct Q {
  int l, r;
  int idx;
};
vector<Q> qs;

void solve() {
  cin >> n; sqn = max(1, (int) sqrt(n));
  a = decltype(a)(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  cin >> nQueries;
  qs = decltype(qs)(nQueries);
  for (int i = 0; i < nQueries; ++i) {
    int l, r; cin >> l >> r; --l; --r;
    qs[i].l = l; qs[i].r = r; qs[i].idx = i;
  }
  sort(begin(qs), end(qs), [](Q a, Q b) {
    if (a.l / sqn != b.l / sqn) {
      return a.l - b.l;
    }
    return a.r - b.r;
  });

  map<int, int> cnt;
  vector<set<int>> vs(n + 1);
  vector<int> ans(nQueries);

  auto inc = [&](int v) {
    if (cnt[v] < 0) return;
    vs[cnt[v]].erase(v);
    ++cnt[v];
    vs[cnt[v]].insert(v);
  };
  auto dec = [&](int v) {
    // db(v, cnt[v]);
    if (cnt[v] <= 0) return;
    vs[cnt[v]].erase(v);
    --cnt[v];
    vs[cnt[v]].insert(v);
  };

  int l = 0, r = -1;
  for (int i = 0; i < nQueries; ++i) {
    auto q = qs[i];
    while (r < q.r) { ++r; inc(a[r]); }
    while (l > q.l) { --l; inc(a[l]); }
    while (r > q.r) { dec(a[r]); --r; }
    while (l < q.l) { dec(a[l]); ++l; }
    if (vs[1].empty()) ans[q.idx] = 0;
    else ans[q.idx] = *vs[1].begin();
  }
  for (int i = 0; i < nQueries; ++i) cout << ans[i] << endl;
}

int main(int argc, char* argv[]) {
  ios::sync_with_stdio(false); cin.tie(nullptr);
  solve();
  return 0;
}


/*
  Task: F. One Occurrence
  Link: http://codeforces.com/contest/1000/problem/F
*/
#include <bits/stdc++.h>
using namespace std;

void err(istream_iterator<string> it) { cerr << endl; }
template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << ", "; err(++it, args...); }
#define db(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define dba(ar) { cerr << #ar << " = ["; for (auto x : ar) cerr << " " << x; cerr << "]" << endl; }
typedef long long int64;
#define ret(x) { cout << x << endl; return; }

int n, nQueries, sqn;
vector<int> a;

struct Q {
  int l, r;
  int idx;
};
vector<Q> qs;

void solve() {
  cin >> n; sqn = max(1, (int) sqrt(n));
  a = decltype(a)(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  cin >> nQueries;
  qs = decltype(qs)(nQueries);
  for (int i = 0; i < nQueries; ++i) {
    int l, r; cin >> l >> r; --l; --r;
    qs[i].l = l; qs[i].r = r; qs[i].idx = i;
  }
  sort(begin(qs), end(qs), [](Q a, Q b) {
    if (a.l / sqn != b.l / sqn) {
      return a.l - b.l;
    }
    return a.r - b.r;
  });

  map<int, int> cnt;
  vector<set<int>> vs(n + 1);
  vector<int> ans(nQueries);

  auto inc = [&](int v) {
    if (cnt[v] < 0) return;
    vs[cnt[v]].erase(v);
    ++cnt[v];
    vs[cnt[v]].insert(v);
  };
  auto dec = [&](int v) {
    // db(v, cnt[v]);
    if (cnt[v] < 0) return;
    vs[cnt[v]].erase(v);
    --cnt[v];
    if (cnt[v] < 0) return;
    vs[cnt[v]].insert(v);
  };

  int l = 0, r = -1;
  for (int i = 0; i < nQueries; ++i) {
    auto q = qs[i];
    while (r < q.r) { ++r; inc(a[r]); }
    while (l > q.l) { --l; inc(a[l]); }
    while (r > q.r) { dec(a[r]); --r; }
    while (l < q.l) { dec(a[l]); ++l; }
    if (vs[1].empty()) ans[q.idx] = 0;
    else ans[q.idx] = *vs[1].begin();
  }
  for (int i = 0; i < nQueries; ++i) cout << ans[i] << endl;
}

int main(int argc, char* argv[]) {
  ios::sync_with_stdio(false); cin.tie(nullptr);
  solve();
  return 0;
}