/*
  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.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]);
    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;
}


/*
  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]);
    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 = 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);
  vector<int> ans(nQueries);

  auto inc = [&](int v) {
    vs[cnt[v]].erase(v);
    ++cnt[v];
    vs[cnt[v]].insert(v);
  };
  auto dec = [&](int 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) { inc(a[r + 1]); ++r; }
    while (r > q.r) { dec(a[r]); --r; }
    while (l < q.l) { dec(a[l]); ++l; }
    while (l > q.l) { inc(a[l - 1]); --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;

  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.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
*/
#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];
  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 = 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;
  });

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

  auto inc = [&](int v) {
    vs[cnt[v]].erase(v);
    vs[++cnt[v]].insert(v);
  };
  auto dec = [&](int v) {
    vs[cnt[v]].erase(v);
    vs[--cnt[v]].insert(v);
  };

  int l = 0, r = -1;
  for (int i = 0; i < nQueries; ++i) {
    auto q = qs[i];
    try {
      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();
    } catch (const exception& e) {
      cout << e.what() << endl;
      ans[q.idx] = 0;
    }
  }
  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;
    if (cd[curAns] != v) cd[++curAns] = v;
  }
};
inline void dec(int v) {
  --cnt[v];
  if (cnt[v] == 1) {
    can[v] = true;
    if (cd[curAns] != v) 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 = 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;
  });

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

  auto 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);
  };
  auto 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);
  };

  int l = 0, r = -1;
  for (int i = 0; i < nQueries; ++i) {
    auto q = qs[i];
    try {
      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();
    } catch (const exception& e) {
      cout << e.what() << endl;
      ans[q.idx] = 0;
    }
  }
  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;
}


# include <stdio.h>
# include <bits/stdc++.h>

#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(),x.end()
#define F first
#define S second
#define For(i,x,y)  for (ll i = x; i <= y; i ++)
#define FOr(i,x,y)  for (ll i = x; i >= y; i --)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0)
// ROAD to...                                                                                Red

using namespace std;

inline bool isvowel (char c) {
	c = tolower(c);
    if (c == 'a' || c == 'e' || c == 'i' || c == 'y' || c == 'o' || c == 'u') return 1;
    return 0;
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 5e5 + 123;
const int M = 22;
const int pri = 997;
const int Magic = 751;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int n, m, k;
int a[N];
vector < int > all;
int was[N];
int cnt[N];
int ans[N];

struct query {
	int l, r, id;
	bool operator < (const query &b) const {
		if (l / Magic == b.l / Magic) return ((l/Magic) % 2 ? r > b.r : r < b.r);
		return l < b.l;
	}
} q[N];

void add (int x) {
	cnt[x] ++;
	if (cnt[x] == 1 && !was[x]) {
		was[x] = 1;
		all.pb(x);
	}

}
void del (int x) {
	cnt[x] --;
	if (cnt[x] == 1 && !was[x]) {
		was[x] = 1;
		all.pb(x);
	}
}

int main () {
	SpeedForce;
    cin >> n;
    For (i, 1, n) cin >> a[i];
   	all.reserve(n + 1);

    cin >> m;
    For (i, 1, m) {
    	cin >> q[i].l >> q[i].r;

    	q[i].id = i;
    }

    sort(q + 1, q + m + 1);
    int L = 1, R = 1;
    add (a[1]);
    For (i, 1, m) {
    	while (L < q[i].l) del (a[L ++]);
    	while (L > q[i].l) add (a[-- L]);
    	while (R < q[i].r) add (a[++ R]);
    	while (R > q[i].r) del (a[R --]);

    	while (all.size() && cnt[all.back()] != 1) {
    		was[all.back()] = 0;
    		all.pop_back();
    	}

    	if (all.size()) ans[q[i].id] = all.back();
    }

    For (i, 1, m) {
    	cout << ans[i] << '\n';
    }

    return Accepted;
}

// Coded By OB


#include<bits/stdc++.h>

#define _USE_MATH_DEFINES
#define ll long long
#define ins Not Needed Thing
#define ull unsigned long long
#define ld long double
#define Accepted 0
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define sz(x) (int)(x.size())
#define all(x) x.begin(),x.end()
#define F first
#define S second
#define emp What is it
#define SORRY FUL Accepted
#define dimash the top one
#define I wish
#define you be a little more calm
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0)

using namespace std;

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 5e3 + 123;
const int NN = 1e6 + 123;
const int PRI = 555557;
const int pri = 997;

int tests = 1;
int n, m, k, a[NN], ans, res[NN], bl[NN];
vector <pair <int, pair <int, int> > > q;
bool cmp (pair <int, pair <int, int> > x, pair <int, pair <int, int> > y) {
	if (bl[x.S.F] == bl[y.S.F]) return x.S.S < y.S.S;
	return bl[x.S.F] < bl[y.S.F];
}
int cnt[NN];
vector < int > s;
int in[N];

void add (int x) {
	cnt[a[x]] ++;
	if (cnt[a[x]] == 1 && !in[a[x]]) {
		s.pb(a[x]);
		in[a[x]] = 1;
	}
}

void del (int x) {
	cnt[a[x]] --;
	if (cnt[a[x]] == 1 && !in[a[x]]) {
		s.pb(a[x]);
		in[a[x]] = 1;
	}
}

inline void Solve () {
	// easy
	cin >> n;
	k = 453;
	for (int i = 1;i <= n; i ++) {
		cin >> a[i];
		bl[i] = i / k;
	}
	cin >> m;
	for (int i = 1; i <= m; i ++) {
		int x, y;
		cin >> x >> y;
		q.pb(mp(i, mp(x, y)));
	}

	sort(q.begin(), q.end(), &cmp);
	int L = 1, R = 1;
	add (1);
	for (int i = 0; i < m; i ++) {
		int l = q[i].S.F, r = q[i].S.S, id = q[i].F;
		while(R < r) {
   			add(++ R);
  		}
  		while(L < l) {
   			del(L ++);
  		}
  		while(L > l) {
   			add(-- L);
  		}
  		while(R > r) {
   			del(R --);
  		}

  		while(s.size() && cnt[s.back()] != 1) {
			in[s.back()] = 0;
			s.pop_back();
		}
  		if (s.size())
			res[id] = s.back();
	}

	for (int i = 1; i <= m; i ++)
		cout << res[i] << '\n';
}

int main () {
	SpeedForce;
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	// cin >> tests;
	while(tests --) {
		Solve ();
		// Ee Zadrot
	}
}
// Why it's so easy, but I can't solve


#include<bits/stdc++.h>

#define _USE_MATH_DEFINES
#define ll long long
#define ins Not Needed Thing
#define ull unsigned long long
#define ld long double
#define Accepted 0
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define sz(x) (int)(x.size())
#define all(x) x.begin(),x.end()
#define F first
#define S second
#define emp What is it
#define SORRY FUL Accepted
#define dimash the top one
#define I wish
#define you be a little more calm
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0)

using namespace std;

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 5e3 + 123;
const int NN = 5e5 + 123;
const int PRI = 555557;
const int pri = 997;

int tests = 1;
int n, m, k, a[NN], ans, res[NN], bl[NN];
vector <pair <int, pair <int, int> > > q;
bool cmp (pair <int, pair <int, int> > x, pair <int, pair <int, int> > y) {
	if (bl[x.S.F] == bl[y.S.F]) return x.S.S < y.S.S;
	return bl[x.S.F] < bl[y.S.F];
}
int cnt[NN];
vector < int > s;
int in[NN];

void add (int x) {
	cnt[a[x]] ++;
	if (cnt[a[x]] == 1 && !in[a[x]]) {
		s.pb(a[x]);
		in[a[x]] = 1;
	}
}

void del (int x) {
	cnt[a[x]] --;
	if (cnt[a[x]] == 1 && !in[a[x]]) {
		s.pb(a[x]);
		in[a[x]] = 1;
	}
}

inline void Solve () {
	// easy
	cin >> n;
	k = sqrt(n) + 1;
	for (int i = 1;i <= n; i ++) {
		cin >> a[i];
		bl[i] = i / k;
	}
	cin >> m;
	for (int i = 1; i <= m; i ++) {
		int x, y;
		cin >> x >> y;
		q.pb(mp(i, mp(x, y)));
	}

	sort(q.begin(), q.end(), &cmp);
	int L = 1, R = 1;
	add (1);
	for (int i = 0; i < m; i ++) {
		int l = q[i].S.F, r = q[i].S.S, id = q[i].F;
		while(R < r) {
   			add(++ R);
  		}
  		while(L < l) {
   			del(L ++);
  		}
  		while(L > l) {
   			add(-- L);
  		}
  		while(R > r) {
   			del(R --);
  		}

  		while (s.size() && cnt[s.back()] != 1) {
  			in[s.back()] = 0;
  			s.pop_back();
  		}
  		if (s.size())
			res[id] = s.back();
	}

	for (int i = 1; i <= m; i ++)
		cout << res[i] << '\n';
}

int main () {
	SpeedForce;
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	// cin >> tests;
	while(tests --) {
		Solve ();
		// Ee Zadrot
	}
}
// Why it's so easy, but I can't solve


#include<bits/stdc++.h>

#define _USE_MATH_DEFINES
#define ll long long
#define ins Not Needed Thing
#define ull unsigned long long
#define ld long double
#define Accepted 0
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define sz(x) (int)(x.size())
#define all(x) x.begin(),x.end()
#define F first
#define S second
#define emp What is it
#define SORRY FUL Accepted
#define dimash the top one
#define I wish
#define you be a little more calm
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0)

using namespace std;

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 5e3 + 123;
const int NN = 1e6 + 123;
const int PRI = 555557;
const int pri = 997;

int tests = 1;
int n, m, k, a[NN], ans, res[NN], bl[NN];
vector <pair <int, pair <int, int> > > q;
bool cmp (pair <int, pair <int, int> > x, pair <int, pair <int, int> > y) {
	if (bl[x.S.F] == bl[y.S.F]) return x.S.S < y.S.S;
	return bl[x.S.F] < bl[y.S.F];
}
int cnt[NN];
vector < int > s;
int in[N];

void add (int x) {
	cnt[a[x]] ++;
	if (cnt[a[x]] == 1 && !in[a[x]]) {
		s.pb(a[x]);
		in[a[x]] = 1;
	}
}

void del (int x) {
	cnt[a[x]] --;
	if (cnt[a[x]] == 1 && !in[a[x]]) {
		s.pb(a[x]);
		in[a[x]] = 1;
	}
}

inline void Solve () {
	// easy
	cin >> n;
	k = 453;
	for (int i = 1;i <= n; i ++) {
		cin >> a[i];
		bl[i] = i / k;
	}
	cin >> m;
	for (int i = 1; i <= m; i ++) {
		int x, y;
		cin >> x >> y;
		q.pb(mp(i, mp(x, y)));
	}

	sort(q.begin(), q.end(), &cmp);
	int L = 1, R = 1;
	add (1);
	for (int i = 0; i < m; i ++) {
		int l = q[i].S.F, r = q[i].S.S, id = q[i].F;
		while(R < r) {
   			add(++ R);
  		}
  		while(L < l) {
   			del(L ++);
  		}
  		while(L > l) {
   			add(-- L);
  		}
  		while(R > r) {
   			del(R --);
  		}

  		while (s.size() && cnt[s.back()] != 1) {
  			in[s.back()] = 0;
  			s.pop_back();
  		}
  		if (s.size())
			res[id] = s.back();
	}

	for (int i = 1; i <= m; i ++)
		cout << res[i] << '\n';
}

int main () {
	SpeedForce;
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	// cin >> tests;
	while(tests --) {
		Solve ();
		// Ee Zadrot
	}
}
// Why it's so easy, but I can't solve


#include<bits/stdc++.h>

#define _USE_MATH_DEFINES
#define ll long long
#define ins Not Needed Thing
#define ull unsigned long long
#define ld long double
#define Accepted 0
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define sz(x) (int)(x.size())
#define all(x) x.begin(),x.end()
#define F first
#define S second
#define emp What is it
#define SORRY FUL Accepted
#define dimash the top one
#define I wish
#define you be a little more calm
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0)

using namespace std;

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 5e3 + 123;
const int NN = 1e6 + 123;
const int PRI = 555557;
const int pri = 997;

int tests = 1;
int n, m, k, a[NN], ans, res[NN], bl[NN];
vector <pair <int, pair <int, int> > > q;
bool cmp (pair <int, pair <int, int> > x, pair <int, pair <int, int> > y) {
	if (bl[x.S.F] == bl[y.S.F]) return x.S.S < y.S.S;
	return bl[x.S.F] < bl[y.S.F];
}
int cnt[NN];
vector < int > s;

void add (int x) {
	cnt[a[x]] ++;
	if (cnt[a[x]] == 1) {
		s.pb(a[x]);
	}
}

void del (int x) {
	cnt[a[x]] --;
	if (cnt[a[x]] == 1) s.pb(a[x]);
}

inline void Solve () {
	// easy
	cin >> n;
	k = 453;
	for (int i = 1;i <= n; i ++) {
		cin >> a[i];
		bl[i] = i / k;
	}
	cin >> m;
	for (int i = 1; i <= m; i ++) {
		int x, y;
		cin >> x >> y;
		q.pb(mp(i, mp(x, y)));
	}

	sort(q.begin(), q.end(), &cmp);
	int L = 1, R = 1;
	add (1);
	for (int i = 0; i < m; i ++) {
		int l = q[i].S.F, r = q[i].S.S, id = q[i].F;
		while(R < r) {
   			add(++ R);
  		}
  		while(L < l) {
   			del(L ++);
  		}
  		while(L > l) {
   			add(-- L);
  		}
  		while(R > r) {
   			del(R --);
  		}

  		while (s.size() && cnt[s.back()] != 1) s.pop_back();
  		if (s.size())
			res[id] = s.back();
	}

	for (int i = 1; i <= m; i ++)
		cout << res[i] << '\n';
}

int main () {
	SpeedForce;
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	// cin >> tests;
	while(tests --) {
		Solve ();
		// Ee Zadrot
	}
}
// Why it's so easy, but I can't solve


#include<bits/stdc++.h>

#define _USE_MATH_DEFINES
#define ll long long
#define ins Not Needed Thing
#define ull unsigned long long
#define ld long double
#define Accepted 0
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define sz(x) (int)(x.size())
#define all(x) x.begin(),x.end()
#define F first
#define S second
#define emp What is it
#define SORRY FUL Accepted
#define dimash the top one
#define I wish
#define you be a little more calm
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0)

using namespace std;

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 5e3 + 123;
const int NN = 1e6 + 123;
const int PRI = 555557;
const int pri = 997;

int tests = 1;
int n, m, k, a[NN], ans, res[NN], bl[NN];
vector <pair <int, pair <int, int> > > q;
bool cmp (pair <int, pair <int, int> > x, pair <int, pair <int, int> > y) {
	if (bl[x.S.F] == bl[y.S.F]) return x.S.S < y.S.S;
	return bl[x.S.F] < bl[y.S.F];
}
int cnt[NN];
set < int > s;

void add (int x) {
	cnt[a[x]] ++;
	if (cnt[a[x]] == 1) {
		s.insert(a[x]);
	}
	if (cnt[a[x]] == 2) s.erase(a[x]);
}

void del (int x) {
	cnt[a[x]] --;
	if (cnt[a[x]] == 0) s.erase(a[x]);
	if (cnt[a[x]] == 1) s.insert(a[x]);
}

inline void Solve () {
	// easy
	cin >> n;
	k = 450;
	for (int i = 1;i <= n; i ++) {
		cin >> a[i];
		bl[i] = i / k;
	}
	cin >> m;
	for (int i = 1; i <= m; i ++) {
		int x, y;
		cin >> x >> y;
		q.pb(mp(i, mp(x, y)));
	}

	sort(q.begin(), q.end(), &cmp);
	int L = 1, R = 1;
	add (1);
	for (int i = 0; i < m; i ++) {
		int l = q[i].S.F, r = q[i].S.S, id = q[i].F;
		while(R < r) {
   			add(++ R);
  		}
  		while(L < l) {
   			del(L ++);
  		}
  		while(L > l) {
   			add(-- L);
  		}
  		while(R > r) {
   			del(R --);
  		}

  		if (s.size())
		res[id] = *s.begin();
	}

	for (int i = 1; i <= m; i ++)
		cout << res[i] << '\n';
}

int main () {
	SpeedForce;
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	// cin >> tests;
	while(tests --) {
		Solve ();
		// Ee Zadrot
	}
}


#include<bits/stdc++.h>

#define _USE_MATH_DEFINES
#define ll long long
#define ins Not Needed Thing
#define ull unsigned long long
#define ld long double
#define Accepted 0
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define sz(x) (int)(x.size())
#define all(x) x.begin(),x.end()
#define F first
#define S second
#define emp What is it
#define SORRY FUL Accepted
#define dimash the top one
#define I wish
#define you be a little more calm
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0)

using namespace std;

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 5e3 + 123;
const int NN = 1e6 + 123;
const int PRI = 555557;
const int pri = 997;

int tests = 1;
int n, m, k, a[NN], res[NN], bl[NN];
vector <pair <int, pair <int, int> > > q;
bool cmp (pair <int, pair <int, int> > x, pair <int, pair <int, int> > y) {
	if (bl[x.S.F] == bl[y.S.F]) return x.S.S < y.S.S;
	return bl[x.S.F] < bl[y.S.F];
}
#define prev lol
int cnt[NN];
int prev[N];
int nxt[N];
int ans;

void add (int x) {
	cnt[a[x]] ++;
	if (cnt[a[x]] == 1) {
		prev[a[x]] = ans;
		nxt[ans] = a[x];
		ans = a[x];
	}
	if (cnt[a[x]] == 2) {
		int le = prev[a[x]];
		int ri = nxt[a[x]];
		if (nxt[a[x]])
			ans = nxt[a[x]];
		else
			ans = le;
		nxt[a[x]] = prev[a[x]] = 0;
		nxt[le] = ri;
		prev[ri] = le;
	}
}

void del (int x) {
	cnt[a[x]] --;
	if (cnt[a[x]] == 0) {
		int le = prev[a[x]];
		int ri = nxt[a[x]];
		if (nxt[a[x]])
			ans = nxt[a[x]];
		else
			ans = le;
		nxt[a[x]] = prev[a[x]] = 0;
		nxt[le] = ri;
		prev[ri] = le;
	}
	if (cnt[a[x]] == 1) {
		prev[a[x]] = ans;
		nxt[ans] = a[x];
		ans = a[x];
	}
}

inline void Solve () {
	// easy
	cin >> n;
	k = 350;
	for (int i = 1;i <= n; i ++) {
		cin >> a[i];
		bl[i] = i / k;
	}
	cin >> m;
	for (int i = 1; i <= m; i ++) {
		int x, y;
		cin >> x >> y;
		q.pb(mp(i, mp(x, y)));
	}

	sort(q.begin(), q.end(), &cmp);
	int L = 1, R = 1;
	add (1);
	for (int i = 0; i < m; i ++) {
		int l = q[i].S.F, r = q[i].S.S, id = q[i].F;
		while(R < r) {
   			add(++ R);
  		}
  		while(L < l) {
   			del(L ++);
  		}
  		while(L > l) {
   			add(-- L);
  		}
  		while(R > r) {
   			del(R --);
  		}

  		res[id] = ans;
	}

	for (int i = 1; i <= m; i ++)
		cout << res[i] << '\n';
}

int main () {
	SpeedForce;
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	// cin >> tests;
	while(tests --) {
		Solve ();
		// Ee Zadrot
	}
}


#include<bits/stdc++.h>

#define _USE_MATH_DEFINES
#define ll long long
#define ins Not Needed Thing
#define ull unsigned long long
#define ld long double
#define Accepted 0
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define sz(x) (int)(x.size())
#define all(x) x.begin(),x.end()
#define F first
#define S second
#define emp What is it
#define SORRY FUL Accepted
#define dimash the top one
#define I wish
#define you be a little more calm
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0)

using namespace std;

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 5e3 + 123;
const int NN = 1e6 + 123;
const int PRI = 555557;
const int pri = 997;

int tests = 1;
int n, m, k, a[NN], ans, res[NN], bl[NN];
vector <pair <int, pair <int, int> > > q;
bool cmp (pair <int, pair <int, int> > x, pair <int, pair <int, int> > y) {
	if (bl[x.S.F] == bl[y.S.F]) return x.S.S < y.S.S;
	return bl[x.S.F] < bl[y.S.F];
}
int cnt[NN];
set < int > s;

void add (int x) {
	cnt[a[x]] ++;
	if (cnt[a[x]] == 1) {
		s.insert(x);
	}
	if (cnt[a[x]] == 2) s.erase(a[x]);
}

void del (int x) {
	cnt[a[x]] --;
	if (cnt[a[x]] == 0) s.erase(a[x]);
	if (cnt[a[x]] == 1) s.insert(a[x]);
}

inline void Solve () {
	// easy
	cin >> n;
	k = 450;
	for (int i = 1;i <= n; i ++) {
		cin >> a[i];
		bl[i] = i / k;
	}
	cin >> m;
	for (int i = 1; i <= m; i ++) {
		int x, y;
		cin >> x >> y;
		q.pb(mp(i, mp(x, y)));
	}

	sort(q.begin(), q.end(), &cmp);
	//add(1);
	int L = 0, R = 0;
	for (int i = 0; i < m; i ++) {
		int l = q[i].S.F, r = q[i].S.S, id = q[i].F;
		while(R < r) {
   			add(++ R);
  		}
  		while(L < l) {
   			del(L ++);
  		}
  		while(L > l) {
   			add(-- L);
  		}
  		while(R > r) {
   			del(R --);
  		}

  		if (s.size())
		res[id] = *s.begin();
	}

	for (int i = 1; i <= m; i ++)
		cout << res[i] << '\n';
}

int main () {
	SpeedForce;
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	// cin >> tests;
	while(tests --) {
		Solve ();
		// Ee Zadrot
	}
}


#include<bits/stdc++.h>

#define _USE_MATH_DEFINES
#define ll long long
#define ins Not Needed Thing
#define ull unsigned long long
#define ld long double
#define Accepted 0
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define sz(x) (int)(x.size())
#define all(x) x.begin(),x.end()
#define F first
#define S second
#define emp What is it
#define SORRY FUL Accepted
#define dimash the top one
#define I wish
#define you be a little more calm
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0)

using namespace std;

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 5e3 + 123;
const int NN = 1e6 + 123;
const int PRI = 555557;
const int pri = 997;

int tests = 1;
int n, m, k, a[NN], ans, res[NN], bl[NN];
vector <pair <int, pair <int, int> > > q;
bool cmp (pair <int, pair <int, int> > x, pair <int, pair <int, int> > y) {
	if (bl[x.S.F] == bl[y.S.F]) return x.S.S < y.S.S;
	return bl[x.S.F] < bl[y.S.F];
}
int cnt[NN];
vector < int > s;
int in[NN];

void add (int x) {
	cnt[a[x]] ++;
	if (cnt[a[x]] == 1 && !in[a[x]]) {
		s.pb(a[x]);
		in[a[x]] = 1;
	}
}

void del (int x) {
	cnt[a[x]] --;
	if (cnt[a[x]] == 1 && !in[a[x]]) {
		s.pb(a[x]);
		in[a[x]] = 1;
	}
}

inline void Solve () {
	// easy
	cin >> n;
	k = 453;
	for (int i = 1;i <= n; i ++) {
		cin >> a[i];
		bl[i] = i / k;
	}
	cin >> m;
	for (int i = 1; i <= m; i ++) {
		int x, y;
		cin >> x >> y;
		q.pb(mp(i, mp(x, y)));
	}

	sort(q.begin(), q.end(), &cmp);
	int L = 1, R = 1;
	add (1);
	for (int i = 0; i < m; i ++) {
		int l = q[i].S.F, r = q[i].S.S, id = q[i].F;
		while(R < r) {
   			add(++ R);
  		}
  		while(L < l) {
   			del(L ++);
  		}
  		while(L > l) {
   			add(-- L);
  		}
  		while(R > r) {
   			del(R --);
  		}

  		while (s.size() && cnt[s.back()] != 1) {
  			in[s.back()] = 0;
  			s.pop_back();
  		}
  		if (s.size())
			res[id] = s.back();
	}

	for (int i = 1; i <= m; i ++)
		cout << res[i] << '\n';
}

int main () {
	SpeedForce;
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	// cin >> tests;
	while(tests --) {
		Solve ();
		// Ee Zadrot
	}
}
// Why it's so easy, but I can't solve


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

typedef long long ll;
typedef long double ld;

#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pii pair <int, int>

const int N = 6e5 + 7;
const int M = 2e6 + 7;

int z = 1, n;
pii g[M];
int a[N], l[N], an[N];

struct st {
	int l, r, i;
} b[N];

bool cmp(st x, st y) {
	return x.r <= y.r;
}

pii get(int v, int lr, int rl, int l, int r) {
	if (l > r)
		return {n + 1, 0};
	if (l == lr && r == rl)
		return g[v];
	int mid = (lr + rl) >> 1;
	pii f = get(v << 1, lr, mid, l, min(mid, r));
	pii s = get(v << 1 | 1, mid + 1, rl, max(mid + 1, l), r);
	if (f.f <= s.f)
		return f;
	return s;
}

void upd(int v) {
	if (g[v << 1].f <= g[v << 1 | 1].f)
		g[v] = g[v << 1];
	else
		g[v] = g[v << 1 | 1];
	if (v > 1)
		upd(v / 2);
}

int main(){
	#ifdef ADIKON
		freopen("problem.in", "r", stdin);
		freopen("problem.out", "w", stdout);
	#endif
	scanf("%d", &n);
	while (z < n)
		z <<= 1;
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[i]);
	int m;
	scanf("%d", &m);
	for (int i = 1; i <= m; ++i) {
		scanf("%d %d", &b[i].l, &b[i].r);
		b[i].i = i;
	}
	sort(b + 1, b + 1 + m, cmp);
	for (int i = 1; i <= (z << 1); ++i)
		g[i] = {n + 1, 0};
	int r = 0;
	for (int i = 1; i <= m; ++i) {
		while (r < b[i].r) {
			++r;
			if (l[a[r]]) {
				g[z - 1 + l[a[r]]] = {n + 1, 0};
				upd((z - 1 + l[a[r]]) / 2);
			}
			g[z - 1 + r] = {l[a[r]], a[r]};
			upd((z - 1 + r) / 2);
			l[a[r]] = r;
		}
		pii x = get(1, 1, z, b[i].l, b[i].r);
		if (x.f < b[i].l)
			an[b[i].i] = x.s;
	}
	for (int i = 1; i <= m; ++i)
		printf("%d\n", an[i]);
	return 0;
}


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

typedef long long ll;
typedef long double ld;

#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pii pair <int, int>

const int N = 7e5 + 7;
const int M = 2e6 + 7;

int z = 1, n;
pii g[M];
int a[N], l[N], an[N];

struct st {
	int l, r, i;
} b[N];

bool cmp(st x, st y) {
	return x.r <= y.r;
}

pii get(int v, int lr, int rl, int l, int r) {
	if (l > r)
		return {n + 1, 0};
	if (l == lr && r == rl)
		return g[v];
	int mid = (lr + rl) >> 1;
	pii f = get(v << 1, lr, mid, l, min(mid, r));
	pii s = get(v << 1 | 1, mid + 1, rl, max(mid + 1, l), r);
	if (f.f <= s.f)
		return f;
	return s;
}

void upd(int v) {
	if (g[v << 1].f <= g[v << 1 | 1].f)
		g[v] = g[v << 1];
	else
		g[v] = g[v << 1 | 1];
	if (v > 1)
		upd(v / 2);
}

int main(){
	#ifdef ADIKON
		freopen("problem.in", "r", stdin);
		freopen("problem.out", "w", stdout);
	#endif
	scanf("%d", &n);
	while (z < n)
		z <<= 1;
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[i]);
	int m;
	scanf("%d", &m);
	for (int i = 1; i <= m; ++i) {
		scanf("%d %d", &b[i].l, &b[i].r);
		b[i].i = i;
	}
	sort(b + 1, b + 1 + m, cmp);
	for (int i = 1; i <= (z << 1); ++i)
		g[i] = {n + 1, 0};
	int r = 0;
	for (int i = 1; i <= m; ++i) {
		while (r < b[i].r) {
			++r;
			if (l[a[r]]) {
				g[z - 1 + l[a[r]]] = {n + 1, 0};
				upd((z - 1 + l[a[r]]) / 2);
			}
			g[z - 1 + r] = {l[a[r]], a[r]};
			upd((z - 1 + r) / 2);
			l[a[r]] = r;
		}
		pii x = get(1, 1, z, b[i].l, b[i].r);
		if (x.f < b[i].l)
			an[b[i].i] = x.s;
	}
	for (int i = 1; i <= m; ++i)
		printf("%d\n", an[i]);
	return 0;
}


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

typedef long long ll;
typedef long double ld;

#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pii pair <int, int>

const int N = 5e5 + 7;
const int M = 3e6 + 7;

int z = 1, n;
pii g[M];
int a[N], l[N], an[N];

struct st {
	int l, r, i;
} b[N];

bool cmp(st x, st y) {
	return x.r <= y.r;
}

pii get(int v, int lr, int rl, int l, int r) {
	if (l > r)
		return {n + 1, 0};
	if (l == lr && r == rl)
		return g[v];
	int mid = (lr + rl) >> 1;
	pii f = get(v << 1, lr, mid, l, min(mid, r));
	pii s = get(v << 1 | 1, mid + 1, rl, max(mid + 1, l), r);
	if (f.f <= s.f)
		return f;
	return s;
}

void upd(int v) {
	if (g[v << 1].f <= g[v << 1 | 1].f)
		g[v] = g[v << 1];
	else
		g[v] = g[v << 1 | 1];
	if (v > 1)
		upd(v / 2);
}

int main(){
	#ifdef ADIKON
		freopen("problem.in", "r", stdin);
		freopen("problem.out", "w", stdout);
	#endif
	scanf("%d", &n);
	while (z < n)
		z <<= 1;
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[i]);
	int m;
	scanf("%d", &m);
	for (int i = 1; i <= m; ++i) {
		scanf("%d %d", &b[i].l, &b[i].r);
		b[i].i = i;
	}
	sort(b + 1, b + 1 + m, cmp);
	for (int i = 1; i <= (z << 1); ++i)
		g[i] = {n + 1, 0};
	int r = 0;
	for (int i = 1; i <= m; ++i) {
		while (r < b[i].r) {
			++r;
			if (l[a[r]]) {
				g[z - 1 + l[a[r]]] = {n + 1, 0};
				upd((z - 1 + l[a[r]]) / 2);
			}
			g[z - 1 + r] = {l[a[r]], a[r]};
			upd((z - 1 + r) / 2);
			l[a[r]] = r;
		}
		pii x = get(1, 1, z, b[i].l, b[i].r);
		if (x.f < b[i].l)
			an[b[i].i] = x.s;
	}
	for (int i = 1; i <= m; ++i)
		printf("%d\n", an[i]);
	return 0;
}


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

typedef long long ll;
typedef long double ld;

#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pii pair <int, int>

const int N = 5e5 + 7;
const int M = 1500000;

int z = 1, n;
pii g[M];
int a[N], l[N], an[N];

struct st {
	int l, r, i;
} b[N];

bool cmp(st x, st y) {
	return x.r < y.r;
}

pii get(int v, int lr, int rl, int l, int r) {
	if (l > r)
		return {n + 1, 0};
	if (l == lr && r == rl)
		return g[v];
	int mid = (lr + rl) >> 1;
	pii f = get(v << 1, lr, mid, l, min(mid, r));
	pii s = get(v << 1 | 1, mid + 1, rl, max(mid + 1, l), r);
	if (f.f < s.f)
		return f;
	return s;
}

void upd(int v) {
	if (g[v << 1].f < g[v << 1 | 1].f)
		g[v] = g[v << 1];
	else
		g[v] = g[v << 1 | 1];
	if (v > 1)
		upd(v / 2);
}

int main(){
	#ifdef ADIKON
		freopen("problem.in", "r", stdin);
		freopen("problem.out", "w", stdout);
	#endif
	scanf("%d", &n);
	while (z < n)
		z <<= 1;
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[i]);
	int m;
	scanf("%d", &m);
	for (int i = 1; i <= m; ++i) {
		scanf("%d %d", &b[i].l, &b[i].r);
		b[i].i = i;
	}
	sort(b + 1, b + 1 + m, cmp);
	memset(g, n + 1, sizeof(g));
	int r = 0;
	for (int i = 1; i <= m; ++i) {
		while (r < b[i].r) {
			++r;
			if (l[a[r]]) {
				g[z - 1 + l[a[r]]] = {n + 1, 0};
				upd((z - 1 + l[a[r]]) / 2);
			}
			g[z - 1 + r] = {l[a[r]], a[r]};
			upd((z - 1 + r) / 2);
			l[a[r]] = r;
		}
		pii x = get(1, 1, z, b[i].l, b[i].r);
		if (x.f < b[i].l)
			an[b[i].i] = x.s;
	}
	for (int i = 1; i <= m; ++i)
		printf("%d\n", an[i]);
	return 0;
}


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

typedef long long ll;
typedef long double ld;

#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pii pair <int, int>

const int N = 5e5 + 7;
const int M = 2000000;

int z = 1, n;
pii g[M];
int a[N], l[N], an[N];

struct st {
	int l, r, i;
} b[N];

bool cmp(st x, st y) {
	return x.r <= y.r;
}

pii get(int v, int lr, int rl, int l, int r) {
	if (l > r)
		return {n + 1, 0};
	if (l == lr && r == rl)
		return g[v];
	int mid = (lr + rl) >> 1;
	pii f = get(v << 1, lr, mid, l, min(mid, r));
	pii s = get(v << 1 | 1, mid + 1, rl, max(mid + 1, l), r);
	if (f.f <= s.f)
		return f;
	return s;
}

void upd(int v) {
	if (g[v << 1].f <= g[v << 1 | 1].f)
		g[v] = g[v << 1];
	else
		g[v] = g[v << 1 | 1];
	if (v > 1)
		upd(v / 2);
}

int main(){
	#ifdef ADIKON
		freopen("problem.in", "r", stdin);
		freopen("problem.out", "w", stdout);
	#endif
	scanf("%d", &n);
	while (z < n)
		z <<= 1;
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[i]);
	int m;
	scanf("%d", &m);
	for (int i = 1; i <= m; ++i) {
		scanf("%d %d", &b[i].l, &b[i].r);
		b[i].i = i;
	}
	sort(b + 1, b + 1 + m, cmp);
	for (int i = 1; i <= (z << 1); ++i)
		g[i] = {n + 1, 0};
	int r = 0;
	for (int i = 1; i <= m; ++i) {
		while (r < b[i].r) {
			++r;
			if (l[a[r]]) {
				g[z - 1 + l[a[r]]] = {n + 1, 0};
				upd((z - 1 + l[a[r]]) / 2);
			}
			g[z - 1 + r] = {l[a[r]], a[r]};
			upd((z - 1 + r) / 2);
			l[a[r]] = r;
		}
		pii x = get(1, 1, z, b[i].l, b[i].r);
		if (x.f < b[i].l)
			an[b[i].i] = x.s;
	}
	for (int i = 1; i <= m; ++i)
		printf("%d\n", an[i]);
	return 0;
}


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

typedef long long ll;
typedef long double ld;

#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pii pair <int, int>

const int N = 5e5 + 7;
const int M = 1500000;

int z = 1, n;
pii g[M];
int a[N], l[N], an[N];

struct st {
	int l, r, i;
} b[N];

bool cmp(st x, st y) {
	return x.r <= y.r;
}

pii get(int v, int lr, int rl, int l, int r) {
	if (l > r)
		return {n + 1, 0};
	if (l == lr && r == rl)
		return g[v];
	int mid = (lr + rl) >> 1;
	pii f = get(v << 1, lr, mid, l, min(mid, r));
	pii s = get(v << 1 | 1, mid + 1, rl, max(mid + 1, l), r);
	if (f.f <= s.f)
		return f;
	return s;
}

void upd(int v) {
	if (g[v << 1].f <= g[v << 1 | 1].f)
		g[v] = g[v << 1];
	else
		g[v] = g[v << 1 | 1];
	if (v > 1)
		upd(v / 2);
}

int main(){
	#ifdef ADIKON
		freopen("problem.in", "r", stdin);
		freopen("problem.out", "w", stdout);
	#endif
	scanf("%d", &n);
	while (z < n)
		z <<= 1;
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[i]);
	int m;
	scanf("%d", &m);
	for (int i = 1; i <= m; ++i) {
		scanf("%d %d", &b[i].l, &b[i].r);
		b[i].i = i;
	}
	sort(b + 1, b + 1 + m, cmp);
	for (int i = 1; i <= (z << 1); ++i)
		g[i] = {n + 1, 0};
	int r = 0;
	for (int i = 1; i <= m; ++i) {
		while (r < b[i].r) {
			++r;
			if (l[a[r]]) {
				g[z - 1 + l[a[r]]] = {n + 1, 0};
				upd((z - 1 + l[a[r]]) / 2);
			}
			g[z - 1 + r] = {l[a[r]], a[r]};
			upd((z - 1 + r) / 2);
			l[a[r]] = r;
		}
		pii x = get(1, 1, z, b[i].l, b[i].r);
		if (x.f < b[i].l)
			an[b[i].i] = x.s;
	}
	for (int i = 1; i <= m; ++i)
		printf("%d\n", an[i]);
	return 0;
}


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

typedef long long ll;
typedef long double ld;

#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pii pair <int, int>

const int N = 7e5 + 7;
const int M = 3e6 + 7;

int z = 1, n;
pii g[M];
int a[N], l[N], an[N];

struct st {
	int l, r, i;
} b[N];

bool cmp(st x, st y) {
	return x.r <= y.r;
}

pii get(int v, int lr, int rl, int l, int r) {
	if (l > r)
		return {n + 1, 0};
	if (l == lr && r == rl)
		return g[v];
	int mid = (lr + rl) >> 1;
	pii f = get(v << 1, lr, mid, l, min(mid, r));
	pii s = get(v << 1 | 1, mid + 1, rl, max(mid + 1, l), r);
	if (f.f <= s.f)
		return f;
	return s;
}

void upd(int v) {
	if (g[v << 1].f <= g[v << 1 | 1].f)
		g[v] = g[v << 1];
	else
		g[v] = g[v << 1 | 1];
	if (v > 1)
		upd(v / 2);
}

int main(){
	#ifdef ADIKON
		freopen("problem.in", "r", stdin);
		freopen("problem.out", "w", stdout);
	#endif
	scanf("%d", &n);
	while (z < n)
		z <<= 1;
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[i]);
	int m;
	scanf("%d", &m);
	for (int i = 1; i <= m; ++i) {
		scanf("%d %d", &b[i].l, &b[i].r);
		b[i].i = i;
	}
	sort(b + 1, b + 1 + m, cmp);
	for (int i = 1; i <= (z << 1); ++i)
		g[i] = {n + 1, 0};
	int r = 0;
	for (int i = 1; i <= m; ++i) {
		while (r < b[i].r) {
			++r;
			if (l[a[r]]) {
				g[z - 1 + l[a[r]]] = {n + 1, 0};
				upd((z - 1 + l[a[r]]) / 2);
			}
			g[z - 1 + r] = {l[a[r]], a[r]};
			upd((z - 1 + r) / 2);
			l[a[r]] = r;
		}
		pii x = get(1, 1, z, b[i].l, b[i].r);
		if (x.f < b[i].l)
			an[b[i].i] = x.s;
	}
	for (int i = 1; i <= m; ++i)
		printf("%d\n", an[i]);
	return 0;
}


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

typedef long long ll;
typedef long double ld;

#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pii pair <int, int>

const int N = 5e5 + 7;
const int M = 2500000;

int z = 1, n;
pii g[M];
int a[N], l[N], an[N];

struct st {
	int l, r, i;
} b[N];

bool cmp(st x, st y) {
	return x.r <= y.r;
}

pii get(int v, int lr, int rl, int l, int r) {
	if (l > r)
		return {n + 1, 0};
	if (l == lr && r == rl)
		return g[v];
	int mid = (lr + rl) >> 1;
	pii f = get(v << 1, lr, mid, l, min(mid, r));
	pii s = get(v << 1 | 1, mid + 1, rl, max(mid + 1, l), r);
	if (f.f <= s.f)
		return f;
	return s;
}

void upd(int v) {
	if (g[v << 1].f <= g[v << 1 | 1].f)
		g[v] = g[v << 1];
	else
		g[v] = g[v << 1 | 1];
	if (v > 1)
		upd(v / 2);
}

int main(){
	#ifdef ADIKON
		freopen("problem.in", "r", stdin);
		freopen("problem.out", "w", stdout);
	#endif
	scanf("%d", &n);
	while (z < n)
		z <<= 1;
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[i]);
	int m;
	scanf("%d", &m);
	for (int i = 1; i <= m; ++i) {
		scanf("%d %d", &b[i].l, &b[i].r);
		b[i].i = i;
	}
	sort(b + 1, b + 1 + m, cmp);
	for (int i = 1; i <= (z << 1); ++i)
		g[i] = {n + 1, 0};
	int r = 0;
	for (int i = 1; i <= m; ++i) {
		while (r < b[i].r) {
			++r;
			if (l[a[r]]) {
				g[z - 1 + l[a[r]]] = {n + 1, 0};
				upd((z - 1 + l[a[r]]) / 2);
			}
			g[z - 1 + r] = {l[a[r]], a[r]};
			upd((z - 1 + r) / 2);
			l[a[r]] = r;
		}
		pii x = get(1, 1, z, b[i].l, b[i].r);
		if (x.f < b[i].l)
			an[b[i].i] = x.s;
	}
	for (int i = 1; i <= m; ++i)
		printf("%d\n", an[i]);
	return 0;
}


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

typedef long long ll;
typedef long double ld;

#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pii pair <int, int>

const int N = 5e5 + 7;
const int M = 2e6 + 7;

int z = 1, n;
pii g[M];
int a[N], l[N], an[N];

struct st {
	int l, r, i;
} b[N];

bool cmp(st x, st y) {
	return x.r <= y.r;
}

pii get(int v, int lr, int rl, int l, int r) {
	if (l > r)
		return {n + 1, 0};
	if (l == lr && r == rl)
		return g[v];
	int mid = (lr + rl) >> 1;
	pii f = get(v << 1, lr, mid, l, min(mid, r));
	pii s = get(v << 1 | 1, mid + 1, rl, max(mid + 1, l), r);
	if (f.f <= s.f)
		return f;
	return s;
}

void upd(int v) {
	if (g[v << 1].f <= g[v << 1 | 1].f)
		g[v] = g[v << 1];
	else
		g[v] = g[v << 1 | 1];
	if (v > 1)
		upd(v / 2);
}

int main(){
	#ifdef ADIKON
		freopen("problem.in", "r", stdin);
		freopen("problem.out", "w", stdout);
	#endif
	scanf("%d", &n);
	while (z < n)
		z <<= 1;
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[i]);
	int m;
	scanf("%d", &m);
	for (int i = 1; i <= m; ++i) {
		scanf("%d %d", &b[i].l, &b[i].r);
		b[i].i = i;
	}
	sort(b + 1, b + 1 + m, cmp);
	for (int i = 1; i <= (z << 1); ++i)
		g[i] = {n + 1, 0};
	int r = 0;
	for (int i = 1; i <= m; ++i) {
		while (r < b[i].r) {
			++r;
			if (l[a[r]]) {
				g[z - 1 + l[a[r]]] = {n + 1, 0};
				upd((z - 1 + l[a[r]]) / 2);
			}
			g[z - 1 + r] = {l[a[r]], a[r]};
			upd((z - 1 + r) / 2);
			l[a[r]] = r;
		}
		pii x = get(1, 1, z, b[i].l, b[i].r);
		if (x.f < b[i].l)
			an[b[i].i] = x.s;
	}
	for (int i = 1; i <= m; ++i)
		printf("%d\n", an[i]);
	return 0;
}


#include<bits/stdc++.h>

#define ld long double
#define ll unsigned long long int
#define float long double
#define pb push_back
#define mk make_pair
#define mod 1000000007
#define ALL(a) a.begin(),a.end()
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define DEBUG(x) cout<<" x is "<<x<<"\n"
using namespace std;

//inline size_t key( int a,int b,int c,int d) {return a*b*c*d-7707;}
//unordered_map<size_t,bool >vismap;

/*int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; */  // 2 Direction
/*int dx[] = {1,0} , dy[] = {0,1};     */  // 4 Direction
/* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction
/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight Direction
/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction

const int N=1000001;
const int  sqN=1000;
ll ans[N];
ll fre[N];
ll v[N];
ll block[sqN];
ll answer=0;

struct query{
	int l;
	int r;
	int idx;
} Q[N];

bool comp(query &q1,query &q2){
	if(q1.l/sqN !=q2.l/sqN){
		return q1.l/sqN <q2.l/sqN;
	}

	return q1.r<q2.r;
}

void add(int idx){
	ll val=v[idx];
	fre[val]++;
	if(fre[val]==1){answer++;block[val/sqN]++;}
	if(fre[val]==2){answer--;block[val/sqN]--;}
}

void rem(int idx){
	ll val=v[idx];
	fre[val]--;
	if(fre[val]==1){answer++;block[val/sqN]++;}
	if(fre[val]==0){answer--;block[val/sqN]--;}
}

ll solve(){
	if(answer==0){
		return 0;
	}
	for(int i=0;i<sqN;i++){
		if(block[i]>0){
			for(int j=i*sqN;j<i*sqN+sqN;j++){
				if(fre[j]==1){
					return j;
				}
			}
		}
	}
}

int main(){
    fast

    memset(ans,0,sizeof(ans));
    memset(fre,0,sizeof(fre));
    memset(block,0,sizeof(block));

    int n;
    cin>>n;

    for(int i=0;i<n;i++){
    	cin>>v[i];
    }

    int q,l,r;
    cin>>q;
    for(int i=0;i<q;i++){
    	cin>>l>>r;
    	l--;r--;
    	Q[i].l=l;
    	Q[i].r=r;
    	Q[i].idx=i;
    }

    sort(Q,Q+q,comp);

    l=0,r=-1;

    for(int i=0;i<q;i++){

    	int L=Q[i].l;
    	int R=Q[i].r;

    	while(l<L){
    		rem(l);
    		l++;
    	}

    	while(l>L){
    		l--;
    		add(l);
    	}

    	while(r<R){
    		r++;
    		add(r);
    	}

    	while(r>R){
    		rem(r);
    		r--;
    	}

    	ans[Q[i].idx]=solve();

    }

    for(int i=0;i<q;i++){
    	cout<<ans[i]<<"\n";
    }

    return 0;
}


#include<bits/stdc++.h>

#define ld long double
#define ll unsigned long long int
#define float long double
#define pb push_back
#define mk make_pair
#define mod 1000000007
#define ALL(a) a.begin(),a.end()
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define DEBUG(x) cout<<" x is "<<x<<"\n"
using namespace std;

//inline size_t key( int a,int b,int c,int d) {return a*b*c*d-7707;}
//unordered_map<size_t,bool >vismap;

/*int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; */  // 2 Direction
/*int dx[] = {1,0} , dy[] = {0,1};     */  // 4 Direction
/* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction
/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight Direction
/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction

const int N=1000001;
const int  sqN=1000;
ll ans[N];
ll fre[N];
ll v[N];
ll block[sqN];
ll answer=0;

struct query{
	int l;
	int r;
	int idx;
} Q[N];

bool comp(query &q1,query &q2){
	if(q1.l/sqN !=q2.l/sqN){
		return q1.l/sqN <q2.l/sqN;
	}

	return q1.r<q2.r;
}

void add(int idx){
	ll val=v[idx];
	fre[val]++;
	if(fre[val]==1){answer++;block[val/sqN]++;}
	if(fre[val]==2){answer--;block[val/sqN]--;}
}

void rem(int idx){
	ll val=v[idx];
	fre[val]--;
	if(fre[val]==1){answer++;block[val/sqN]++;}
	if(fre[val]==0){answer--;block[val/sqN]--;}
}

ll solve(){
	if(answer==0){
		return 0;
	}
	for(int i=0;i<sqN;i++){
		if(block[i]>0){
			for(int j=i*sqN;j<i*sqN+sqN;j++){
				if(fre[j]>0){
					return j;
				}
			}
		}
	}
}

int main(){
    fast

    memset(ans,0,sizeof(ans));
    memset(fre,0,sizeof(fre));
    memset(block,0,sizeof(block));

    int n;
    cin>>n;

    for(int i=0;i<n;i++){
    	cin>>v[i];
    }

    int q,l,r;
    cin>>q;
    for(int i=0;i<q;i++){
    	cin>>l>>r;
    	l--;r--;
    	Q[i].l=l;
    	Q[i].r=r;
    	Q[i].idx=i;
    }

    sort(Q,Q+q,comp);

    l=0,r=-1;

    for(int i=0;i<q;i++){

    	int L=Q[i].l;
    	int R=Q[i].r;

    	while(l<L){
    		rem(l);
    		l++;
    	}

    	while(l>L){
    		l--;
    		add(l);
    	}

    	while(r<R){
    		r++;
    		add(r);
    	}

    	while(r>R){
    		rem(r);
    		r--;
    	}

    	ans[Q[i].idx]=solve();

    }

    for(int i=0;i<q;i++){
    	cout<<ans[i]<<"\n";
    }

    return 0;
}


#include<bits/stdc++.h>

#define ld long double
#define ll unsigned long long int
#define float long double
#define pb push_back
#define mk make_pair
#define mod 1000000007
#define ALL(a) a.begin(),a.end()
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define DEBUG(x) cout<<" x is "<<x<<"\n"
using namespace std;

//inline size_t key( int a,int b,int c,int d) {return a*b*c*d-7707;}
//unordered_map<size_t,bool >vismap;

/*int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; */  // 2 Direction
/*int dx[] = {1,0} , dy[] = {0,1};     */  // 4 Direction
/* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction
/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight Direction
/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction

const int N=500001;
const int  sqN=708;
ll ans[N];
ll fre[N];
ll v[N];
ll block[sqN];
ll answer=0;

struct query{
	int l;
	int r;
	int idx;
} Q[N];

bool comp(query &q1,query &q2){
	if(q1.l/sqN !=q2.l/sqN){
		return q1.l/sqN <q2.l/sqN;
	}

	return q1.r<q2.r;
}

void add(int idx){
	ll val=v[idx];
	fre[val]++;
	if(fre[val]==1){answer++;block[val/sqN]++;}
	if(fre[val]==2){answer--;block[val/sqN]--;}
}

void rem(int idx){
	ll val=v[idx];
	fre[val]--;
	if(fre[val]==1){answer++;block[val/sqN]++;}
	if(fre[val]==0){answer--;block[val/sqN]--;}
}

ll solve(){
	if(answer==0){
		return 0;
	}
	for(int i=0;i<sqN;i++){
		if(block[i]>0){
			for(int j=i*sqN;j<i*sqN+sqN;j++){
				if(fre[j]==1){
					return j;
				}
			}
		}
	}
}

int main(){
    fast

    memset(ans,0,sizeof(ans));
    memset(fre,0,sizeof(fre));
    memset(block,0,sizeof(block));

    int n;
    cin>>n;

    for(int i=0;i<n;i++){
    	cin>>v[i];
    }

    int q,l,r;
    cin>>q;
    for(int i=0;i<q;i++){
    	cin>>l>>r;
    	l--;r--;
    	Q[i].l=l;
    	Q[i].r=r;
    	Q[i].idx=i;
    }

    sort(Q,Q+q,comp);

    l=0,r=-1;

    for(int i=0;i<q;i++){

    	int L=Q[i].l;
    	int R=Q[i].r;

    	while(l<L){
    		rem(l);
    		l++;
    	}

    	while(l>L){
    		l--;
    		add(l);
    	}

    	while(r<R){
    		r++;
    		add(r);
    	}

    	while(r>R){
    		rem(r);
    		r--;
    	}

    	ans[Q[i].idx]=solve();

    }

    for(int i=0;i<q;i++){
    	cout<<ans[i]<<"\n";
    }

    return 0;
}


#include<bits/stdc++.h>

#define ld long double
#define ll unsigned long long int
#define float long double
#define pb push_back
#define mk make_pair
#define mod 1000000007
#define ALL(a) a.begin(),a.end()
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define DEBUG(x) cout<<" x is "<<x<<"\n"
using namespace std;

//inline size_t key( int a,int b,int c,int d) {return a*b*c*d-7707;}
//unordered_map<size_t,bool >vismap;

/*int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; */  // 2 Direction
/*int dx[] = {1,0} , dy[] = {0,1};     */  // 4 Direction
/* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction
/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight Direction
/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction

const int N=500001;
const int  sqN=708;
ll ans[N];
ll fre[N];
ll v[N];
ll block[sqN];
ll answer=0;

struct query{
	int l;
	int r;
	int idx;
} Q[N];

/*
bool comp(query &q1,query &q2){
	if(q1.l/sqN !=q2.l/sqN){
		return q1.l <q2.l;
	}
	if((q1.l/sqN) &1){
		return q1.r <q2.r;
	}
	return q1.r>q2.r;
}
*/

inline void add(int idx){
	ll val=v[idx];
	fre[val]++;
	if(fre[val]==1){answer++;block[val/sqN]++;}
	if(fre[val]==2){answer--;block[val/sqN]--;}
}

inline void rem(int idx){
	ll val=v[idx];
	fre[val]--;
	if(fre[val]==1){answer++;block[val/sqN]++;}
	if(fre[val]==0){answer--;block[val/sqN]--;}
}

inline ll solve(){
	if(answer==0){
		return 0;
	}
	for(int i=0;i<sqN;i++){
		if(block[i]>0){
			for(int j=i*sqN;j<i*sqN+sqN;j++){
				if(fre[j]==1){
					return j;
				}
			}
		}
	}
}

int main(){
    fast

   // memset(ans,0,sizeof(ans));
   // memset(fre,0,sizeof(fre));
   // memset(block,0,sizeof(block));

    int n;
    cin>>n;

    for(int i=0;i<n;i++){
    	cin>>v[i];
    }

    int q,l,r;
    cin>>q;
    for(int i=0;i<q;i++){
    	cin>>l>>r;
    	l--;r--;
    	Q[i].l=l;
    	Q[i].r=r;
    	Q[i].idx=i;
    }

    sort(Q,Q+q,[](query &q1,query &q2){
            if(q1.l/sqN !=q2.l/sqN){
                return q1.l <q2.l;
            }
        if((q1.l/sqN) &1){
            return q1.r <q2.r;
        }
        return q1.r>q2.r;
         });

    l=0,r=-1;

    for(int i=0;i<q;i++){

    	int L=Q[i].l;
    	int R=Q[i].r;

    	while(l<L){
    		rem(l);
    		l++;
    	}

    	while(l>L){
    		l--;
    		add(l);
    	}

    	while(r<R){
    		r++;
    		add(r);
    	}

    	while(r>R){
    		rem(r);
    		r--;
    	}

    	ans[Q[i].idx]=solve();

    }

    for(int i=0;i<q;i++){
    	cout<<ans[i]<<"\n";
    }

    return 0;
}


#include<bits/stdc++.h>

#define ld long double
#define ll unsigned long long int
#define float long double
#define pb push_back
#define mk make_pair
#define mod 1000000007
#define ALL(a) a.begin(),a.end()
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define DEBUG(x) cout<<" x is "<<x<<"\n"
using namespace std;

//inline size_t key( int a,int b,int c,int d) {return a*b*c*d-7707;}
//unordered_map<size_t,bool >vismap;

/*int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; */  // 2 Direction
/*int dx[] = {1,0} , dy[] = {0,1};     */  // 4 Direction
/* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction
/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight Direction
/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction

const int N=500001;
const int  sqN=708;
ll ans[N];
ll fre[N];
ll v[N];
ll block[sqN];
ll answer=0;

struct query{
	int l;
	int r;
	int idx;
} Q[N];

bool comp(query &q1,query &q2){
	if(q1.l/sqN !=q2.l/sqN){
		return q1.l <q2.l;
	}
	if((q1.l/sqN) &1){
		return q1.r <q2.r;
	}
	return q1.r<q2.r;
}

inline void add(int idx){
	ll val=v[idx];
	fre[val]++;
	if(fre[val]==1){answer++;block[val/sqN]++;}
	if(fre[val]==2){answer--;block[val/sqN]--;}
}

inline void rem(int idx){
	ll val=v[idx];
	fre[val]--;
	if(fre[val]==1){answer++;block[val/sqN]++;}
	if(fre[val]==0){answer--;block[val/sqN]--;}
}

inline ll solve(){
	if(answer==0){
		return 0;
	}
	for(int i=0;i<sqN;i++){
		if(block[i]>0){
			for(int j=i*sqN;j<i*sqN+sqN;j++){
				if(fre[j]==1){
					return j;
				}
			}
		}
	}
}

int main(){
    fast

   // memset(ans,0,sizeof(ans));
   // memset(fre,0,sizeof(fre));
   // memset(block,0,sizeof(block));

    int n;
    cin>>n;

    for(int i=0;i<n;i++){
    	cin>>v[i];
    }

    int q,l,r;
    cin>>q;
    for(int i=0;i<q;i++){
    	cin>>l>>r;
    	l--;r--;
    	Q[i].l=l;
    	Q[i].r=r;
    	Q[i].idx=i;
    }

    sort(Q,Q+q,comp);

    l=0,r=-1;

    for(int i=0;i<q;i++){

    	int L=Q[i].l;
    	int R=Q[i].r;

    	while(l<L){
    		rem(l);
    		l++;
    	}

    	while(l>L){
    		l--;
    		add(l);
    	}

    	while(r<R){
    		r++;
    		add(r);
    	}

    	while(r>R){
    		rem(r);
    		r--;
    	}

    	ans[Q[i].idx]=solve();

    }

    for(int i=0;i<q;i++){
    	cout<<ans[i]<<"\n";
    }

    return 0;
}


#include<bits/stdc++.h>

#define ld long double
#define ll unsigned long long int
#define float long double
#define pb push_back
#define mk make_pair
#define mod 1000000007
#define ALL(a) a.begin(),a.end()
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define DEBUG(x) cout<<" x is "<<x<<"\n"
using namespace std;

//inline size_t key( int a,int b,int c,int d) {return a*b*c*d-7707;}
//unordered_map<size_t,bool >vismap;

/*int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; */  // 2 Direction
/*int dx[] = {1,0} , dy[] = {0,1};     */  // 4 Direction
/* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction
/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight Direction
/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction

const int N=500001;
const int  sqN=708;
ll ans[N];
ll fre[N];
ll v[N];
ll block[sqN];
ll answer=0;

struct query{
	int l;
	int r;
	int idx;
} Q[N];

bool comp(query &q1,query &q2){
	if(q1.l/sqN !=q2.l/sqN){
		return q1.l <q2.l;
	}
	if((q1.l/sqN) &1){
		return q1.l <q2.l;
	}
	return q1.r<q2.r;
}

void add(int idx){
	ll val=v[idx];
	fre[val]++;
	if(fre[val]==1){answer++;block[val/sqN]++;}
	if(fre[val]==2){answer--;block[val/sqN]--;}
}

void rem(int idx){
	ll val=v[idx];
	fre[val]--;
	if(fre[val]==1){answer++;block[val/sqN]++;}
	if(fre[val]==0){answer--;block[val/sqN]--;}
}

ll solve(){
	if(answer==0){
		return 0;
	}
	for(int i=0;i<sqN;i++){
		if(block[i]>0){
			for(int j=i*sqN;j<i*sqN+sqN;j++){
				if(fre[j]==1){
					return j;
				}
			}
		}
	}
}

int main(){
    fast

    memset(ans,0,sizeof(ans));
    memset(fre,0,sizeof(fre));
    memset(block,0,sizeof(block));

    int n;
    cin>>n;

    for(int i=0;i<n;i++){
    	cin>>v[i];
    }

    int q,l,r;
    cin>>q;
    for(int i=0;i<q;i++){
    	cin>>l>>r;
    	l--;r--;
    	Q[i].l=l;
    	Q[i].r=r;
    	Q[i].idx=i;
    }

    sort(Q,Q+q,comp);

    l=0,r=-1;

    for(int i=0;i<q;i++){

    	int L=Q[i].l;
    	int R=Q[i].r;

    	while(l<L){
    		rem(l);
    		l++;
    	}

    	while(l>L){
    		l--;
    		add(l);
    	}

    	while(r<R){
    		r++;
    		add(r);
    	}

    	while(r>R){
    		rem(r);
    		r--;
    	}

    	ans[Q[i].idx]=solve();

    }

    for(int i=0;i<q;i++){
    	cout<<ans[i]<<"\n";
    }

    return 0;
}


#include<bits/stdc++.h>

#define ld long double
#define ll unsigned long long int
#define float long double
#define pb push_back
#define mk make_pair
#define mod 1000000007
#define ALL(a) a.begin(),a.end()
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define DEBUG(x) cout<<" x is "<<x<<"\n"
using namespace std;

//inline size_t key( int a,int b,int c,int d) {return a*b*c*d-7707;}
//unordered_map<size_t,bool >vismap;

/*int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; */  // 2 Direction
/*int dx[] = {1,0} , dy[] = {0,1};     */  // 4 Direction
/* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction
/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight Direction
/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction

const int N=1000001;
const int  sqN=1000;
int ans[N];
int fre[N];
int v[N];
int block[sqN];
int answer=0;

struct query{
	int l;
	int r;
	int idx;
} Q[N];

bool comp(query &q1,query &q2){
	if(q1.l/sqN !=q2.l/sqN){
		return q1.l/sqN <q2.l/sqN;
	}

	return q1.r<q2.r;
}

void add(int idx){
	ll val=v[idx];
	fre[val]++;
	if(fre[val]==1){answer++;block[val/sqN]++;}
	if(fre[val]==2){answer--;block[val/sqN]--;}
}

void rem(int idx){
	ll val=v[idx];
	fre[val]--;
	if(fre[val]==1){answer++;block[val/sqN]++;}
	if(fre[val]==0){answer--;block[val/sqN]--;}
}

int solve(){
	if(answer==0){
		return 0;
	}
	for(int i=0;i<sqN;i++){
		if(block[i]>0){
			for(int j=i*sqN;j<i*sqN+sqN;j++){
				if(fre[j]==1){
					return j;
				}
			}
		}
	}
}

int main(){
    fast

    memset(ans,0,sizeof(ans));
    memset(fre,0,sizeof(fre));
    memset(block,0,sizeof(block));

    int n;
    cin>>n;

    for(int i=0;i<n;i++){
    	cin>>v[i];
    }

    int q,l,r;
    cin>>q;
    for(int i=0;i<q;i++){
    	cin>>l>>r;
    	l--;r--;
    	Q[i].l=l;
    	Q[i].r=r;
    	Q[i].idx=i;
    }

    sort(Q,Q+q,comp);

    l=0,r=-1;

    for(int i=0;i<q;i++){

    	int L=Q[i].l;
    	int R=Q[i].r;

    	while(l<L){
    		rem(l);
    		l++;
    	}

    	while(l>L){
    		l--;
    		add(l);
    	}

    	while(r<R){
    		r++;
    		add(r);
    	}

    	while(r>R){
    		rem(r);
    		r--;
    	}

    	ans[Q[i].idx]=solve();

    }

    for(int i=0;i<q;i++){
    	cout<<ans[i]<<"\n";
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{

    string s;
    cin>>s;
    int k;
    cin>>k;
    int a[26];
   int maxa=INT_MIN;
    for(int i=0;i<26;i++)
    {
        cin>>a[i];
        maxa=max(maxa,a[i]);

    }
    long long res=0;
    int n=s.length();
    for(int i=0;i<n;i++)
    {
        int p=s[i]-97;
        res+=((i+1)*a[p]);
    }
    int tot=n+k;
    long long tres1=(tot*(tot+1))/2;
    long long tres2=(n*(n+1))/2;
    res+=(tres1-tres2)*maxa;
    cout<<res<<endl;
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long int ll;
int main(){
	fast_io
	//freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	string s;cin>>s;
	int k;cin>>k;
	map<char,int>mp;
	int mx=-1;char mxc;
	for(char i='a';i!='z';i++){
		int inp;
		cin>>inp;
		mp[i]=inp;
		if(inp>mx){
			mx=inp;
			mxc=i;
		}
	}
	while(k--){
		s+=mxc;
	}
	int sum=0;
	for(int i=0;i<s.size();i++){
		sum+=(mp[s[i]]*(i+1));
	}
	cout << sum;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long int ll;
int main(){
	fast_io
	//freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	string s;cin>>s;
	int k;cin>>k;
	map<char,ll>mp;
	int mx=-1;char mxc;
	for(char i='a';i<='z';i++){
		int inp;
		cin>>inp;
		mp[i]=inp;
		if(inp>mx){
			mx=inp;
			mxc=i;
		}
	}
	while(k--){
		s+=mxc;
	}//cout << s;
	int sum=0;
	for(int i=0;i<s.size();i++){
		sum+=(mp[s[i]]*(i+1));
	}
	cout << sum;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long int ll;
int main(){
	fast_io
	//freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	string s;cin>>s;
	int k;cin>>k;
	map<char,int>mp;
	int mx=-1;char mxc;
	for(char i='a';i<='z';i++){
		int inp;
		cin>>inp;
		mp[i]=inp;
		if(inp>mx){
			mx=inp;
			mxc=i;
		}
	}
	while(k--){
		s+=mxc;
	}cout << s;
	int sum=0;
	for(int i=0;i<s.size();i++){
		sum+=(mp[s[i]]*(i+1));
	}
	cout << sum;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long int ll;
int main(){
	fast_io
	//freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	string s;cin>>s;
	int k;cin>>k;
	map<char,int>mp;
	int mx=-1;char mxc;
	for(char i='a';i!='z';i++){
		int inp;
		cin>>inp;
		mp[i]=inp;
		if(inp>mx){
			mx=inp;
			mxc=i;
		}
	}
	while(k--){
		s+=mxc;
	}
	int sum=0;
	for(int i=0;i<=s.size();i++){
		sum+=(mp[s[i]]*(i+1));
	}
	cout << sum;
	return 0;
}


#include<iostream>
#include<vector>
#include <stdio.h>
#include<bits/stdc++.h>
#include<algorithm>
#include<cstring>
#include<set>
#include<math.h>
#include <iterator>

using namespace std;

int main(){
        char ch[1001];
        cin>>ch;
        int k;
        cin>>k;
        vector<int> a;
        for(int i=0;i<26;i++){
            int q;
            cin>>q;
            a.push_back(q);
        }
        long s=0;
        for(int i=0;i<strlen(ch);i++){
          //int k=int(ch[i])-96;
          s+=a[i]*(i+1);

        }
        sort(a.begin(),a.end());
        for(int l=strlen(ch)+1;l<=k+strlen(ch);l++){
            s+=a[a.size()-1]*l;
        }
        cout<<s;
        return 0;
}


#include<iostream>
#include<vector>
#include <stdio.h>
#include<bits/stdc++.h>
#include<algorithm>
#include<cstring>
#include<set>
#include<math.h>
#include <iterator>

using namespace std;

int main(){
        char ch[1001];
        cin>>ch;
        int k;
        cin>>k;
        vector<int> a;
        for(int i=0;i<26;i++){
            int q;
            cin>>q;
            a.push_back(q);
        }
        long s=0;
        for(int i=0;i<strlen(ch);i++){
          int k=int(ch[i])-96;
          s+=a[i]*k;

        }
        sort(a.begin(),a.end());
        for(int l=strlen(ch)+1;l<=k+strlen(ch);l++){
            s+=a[a.size()-1]*l;
        }
        cout<<s;
        return 0;
}


#include<iostream>
#include<vector>
#include <stdio.h>
#include<bits/stdc++.h>
#include<algorithm>
#include<cstring>
#include<set>
#include<math.h>
#include <iterator>

using namespace std;

int main(){
        char ch[1001];
        cin>>ch;
        int k;
        cin>>k;
        vector<int> a;
        for(int i=0;i<26;i++){
            int q;
            cin>>q;
            a.push_back(q);
        }
        long s=0;
        for(int i=0;i<strlen(ch);i++){
          int ki=int(ch[i])-97;
          s+=a[ki]*(i+1);

        }
        sort(a.begin(),a.end());
        for(int l=strlen(ch)+1;l<=k+strlen(ch);l++){
            s+=a[a.size()-1]*l;
        }
        cout<<s;
        return 0;
}


                /*In the name of ALLAH, Most Gracious, Most Merciful*/
#include<bits/stdc++.h>

#define ll                      long long int
#define ull                     unsigned long long int
#define PI                      2*acos(0.0)
#define sqr(x)                  (x)*(x)
#define EPS                     10E-10
#define MOD                     1000000007
#define longlimit               92233720368547758
#define infinity                (1<<28)
#define bitcount                __builtin_popcount
#define pb(a)                   emplace_back(a)
#define mp                      make_pair
#define ff                      first
#define ss                      second
#define gcd(a, b)               __gcd(a,b)
#define lcm(a, b)               ((a)*(b))/gcd(a,b)
#define all(a)                  (a.begin()),(a.end())
#define UNIQUE(X)               (X).erase(unique(all(X)),(X).end())
#define DELETE(X)               (X).erase (remove (all(X)),(X).end())
#define SORT_UNIQUE(c)          (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
using namespace std;

int solve( )
{
    string s;
    cin>>s;
    ll k,sum=0,maxi=INT_MIN,w,index;
    cin>>k;
    map<char,int>m;
    for (int i=0;i<26;i++)
    {
        int a;
        cin>>a;
        m['a'+i]=a;
        if(a>maxi)maxi=a;
    }
    //for (auto it:m) cout<<it.ff<<" :"<<it.ss<<endl;
    for (int i=0;i<k;i++)s+=maxi+'a';
    for (int i=0;i<s.size();i++)
    {
        sum+=(i+1)*m[s[i]];
    }
    cout<<sum<<endl;

}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    solve();
}


                /*In the name of ALLAH, Most Gracious, Most Merciful*/
#include<bits/stdc++.h>

#define ll                      long long int
#define ull                     unsigned long long int
#define PI                      2*acos(0.0)
#define sqr(x)                  (x)*(x)
#define EPS                     10E-10
#define MOD                     1000000007
#define longlimit               92233720368547758
#define infinity                (1<<28)
#define bitcount                __builtin_popcount
#define pb(a)                   emplace_back(a)
#define mp                      make_pair
#define ff                      first
#define ss                      second
#define gcd(a, b)               __gcd(a,b)
#define lcm(a, b)               ((a)*(b))/gcd(a,b)
#define all(a)                  (a.begin()),(a.end())
#define UNIQUE(X)               (X).erase(unique(all(X)),(X).end())
#define DELETE(X)               (X).erase (remove (all(X)),(X).end())
#define SORT_UNIQUE(c)          (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    string s,temp;
    cin>>s;
    ll k,sum=0,maxi=INT_MIN,w,index;
    cin>>k;
    int arr[26];
    for (int i=0;i<26;i++)
    {
        cin>>arr[i];
        if(arr[i]>maxi)maxi=arr[i],index=i;
    }
    for (int i=0;i<s.size();i++)
        sum+=(arr[s[i]-'a']*(i+1));
    for (int i=0;i<k;i++)
        temp+=(index+'a');
    for (int i=0;i<temp.size();i++)
        sum+=arr[temp[i]-'a']*(s.size()+i+1);
    cout<<sum<<endl;
}


                /*In the name of ALLAH, Most Gracious, Most Merciful*/
#include<bits/stdc++.h>

#define ll                      long long int
#define ull                     unsigned long long int
#define PI                      2*acos(0.0)
#define sqr(x)                  (x)*(x)
#define EPS                     10E-10
#define MOD                     1000000007
#define longlimit               92233720368547758
#define infinity                (1<<28)
#define bitcount                __builtin_popcount
#define pb(a)                   emplace_back(a)
#define mp                      make_pair
#define ff                      first
#define ss                      second
#define gcd(a, b)               __gcd(a,b)
#define lcm(a, b)               ((a)*(b))/gcd(a,b)
#define all(a)                  (a.begin()),(a.end())
#define UNIQUE(X)               (X).erase(unique(all(X)),(X).end())
#define DELETE(X)               (X).erase (remove (all(X)),(X).end())
#define SORT_UNIQUE(c)          (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
using namespace std;

int solve( )
{
    string s,temp;
    cin>>s;
    ll k,sum=0,maxi=INT_MIN,w,index;
    cin>>k;
    int arr[26];
    for (int i=0;i<26;i++)
    {
        cin>>arr[i];
        if(arr[i]>maxi)maxi=arr[i],index=i;
    }
    for (int i=0;i<s.size();i++)
        sum+=(arr[s[i]-'a']*(i+1));
    for (int i=0;i<k;i++)
        temp+=(index+'a');
    cout<<temp<<endl;
    for (int i=0;i<temp.size();i++)
        sum+=arr[temp[i]-'a']*(s.size()+i+1);
    cout<<sum<<endl;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    solve();
}


#include<iostream>
#include<sstream>
#include<iterator>
#include<cstdio>
#include<algorithm>
#include <random>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
#include <complex>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mt(a,b) memset(a,b,sizeof(a))
#define pii pair<int, int>
#define pdd pair<double, double>
#define pid pair<int, double>
#define pdi pair<double. int>
#define fr(i,a,b) for(int i=a;i<=b;i++)
#define fo(i,a,b) for(int i=a;i<=b;i--)
const int INF  =  0x3f3f3f3f;
const int mod  =  1e9+7;
const int maxn =  1000000+5;
const double PI  =  acos(-1.0);
const double E   =  2.718281828459;
const double eps = 1e-7;
bool cmp(int a,int b){
    return a>b;
}
int main(){
    char a[1005];
    scanf("%s",a);
    int n;
    cin>>n;
    int b[26];
    fr(i,0,25)
        cin>>b[i];
    int sum=0;
    int l=strlen(a);
    fr(i,0,l-1){
        sum+=b[a[i]-'a']*(i+1);
    }
    sort(b,b+25,cmp);
    fr(i,l,l+2)
    sum+=b[0]*(i+1);
    cout<<sum<<endl;
    return 0;
}


#include<iostream>
#include<sstream>
#include<iterator>
#include<cstdio>
#include<algorithm>
#include <random>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
#include <complex>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mt(a,b) memset(a,b,sizeof(a))
#define pii pair<int, int>
#define pdd pair<double, double>
#define pid pair<int, double>
#define pdi pair<double. int>
#define fr(i,a,b) for(int i=a;i<=b;i++)
#define fo(i,a,b) for(int i=a;i<=b;i--)
const int INF  =  0x3f3f3f3f;
const int mod  =  1e9+7;
const int maxn =  1000000+5;
const double PI  =  acos(-1.0);
const double E   =  2.718281828459;
const double eps = 1e-7;
bool cmp(int a,int b){
    return a>b;
}
int main(){
    char a[1005];
    scanf("%s",a);
    int n;
    cin>>n;
    int b[26];
    fr(i,0,25)
        cin>>b[i];
    ll sum=0;
    int l=strlen(a);
    fr(i,0,l-1){
        sum+=b[a[i]-'a']*(i+1);
    }
    sort(b,b+25,cmp);
    fr(i,l,l+n-1)
    sum+=b[0]*(i+1);
    cout<<sum<<endl;
    return 0;
}


#include<iostream>
#include<sstream>
#include<iterator>
#include<cstdio>
#include<algorithm>
#include <random>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
#include <complex>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mt(a,b) memset(a,b,sizeof(a))
#define pii pair<int, int>
#define pdd pair<double, double>
#define pid pair<int, double>
#define pdi pair<double. int>
#define fr(i,a,b) for(int i=a;i<=b;i++)
#define fo(i,a,b) for(int i=a;i<=b;i--)
const int INF  =  0x3f3f3f3f;
const int mod  =  1e9+7;
const int maxn =  1000000+5;
const double PI  =  acos(-1.0);
const double E   =  2.718281828459;
const double eps = 1e-7;
bool cmp(int a,int b){
    return a>b;
}
int main(){
    char a[1005];
    scanf("%s",a);
    int n;
    cin>>n;
    int b[26];
    fr(i,0,25)
        cin>>b[i];
    ll sum=0;
    int l=strlen(a);
    fr(i,0,l-1){
        sum+=b[a[i]-'a']*(i+1);
    }
    sort(b,b+25,cmp);
    fr(i,l,l+2)
    sum+=b[0]*(i+1);
    cout<<sum<<endl;
    return 0;
}


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

int main(){
    string s;cin>>s;
    int k;cin>>k;
    int n=s.length();
    int val[26];for(int i=0;i<26;i++)cin>>val[i];
    int max=0;
    for(int i=0;i<26;i++)max=(val[i]>max)?val[i]:max;
    int res=0;
    for(int i=0;i<n;i++)res+=i*val[s[i]-97];
    res+=max*(k*n+(k*(k+1))/2);
    cout<<res;
    return 0;
}


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

int main(){
    string s;cin>>s;
    int k;cin>>k;
    int n=s.length();
    int val[26];for(int i=0;i<26;i++)cin>>val[i];
    int max=0;
    for(int i=0;i<26;i++)max=(val[i]>max)?val[i]:max;
    int res=0;
    for(int i=0;i<n;i++)res+=i*val[s[i]-97];
    val+=max*(k*n+(k*(k+1))/2);
    cout<<val;
    return 0;
}


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

int main(){
    string s;cin>>s;
    int k;cin>>k;
    int n=s.length();
    int val[26];for(int i=0;i<26;i++)cin>>val[i];
    int max=0;
    for(int i=0;i<26;i++)max=(val[i]>max)?val[i]:max;
    int res=0;
    for(int i=0;i<n;i++)res+=(i+1)*val[s[i]-97];
    res+=max*(k*n+(k*(k+1))/2);
    cout<<res;
    return 0;
}


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

int main(){
    string s;cin>>s;
    int k;cin>>k;
    int val[26];for(int i=0;i<26;i++)cin>>val[i];
    int max=0;
    for(int i=0;i<26;i++)max=(val[i]>max)?val[i]:max;
    int res=0;
    for(int i=0;i<s.length();i++)res+=i*val[s[i]-97];
    val+=max*(k*s.length()+(k*(k+1))/2);
    cout<<val;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<sstream>
#include<string>
#include<algorithm>
#include<math.h>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<list>
#include<set>
#include<map>
#include<bitset>
#include<iterator>
#include <stack>
#define outfile freopen("", "w", stdout);
#define infile freopen("", "r", stdin);
#define ll long long
const ll INF = 2e18;
typedef long double ld;
#define all(v) ((v).begin()),((v).end())
#define vec2(n,m) vector<vector<ll>>vv(n + 10, vector<ll>(m + 10, 0));
#define p_q1   priority_queue<int,vector<int>,greater<int>>pq1;
#define format(n) fixed<<setprecision(n)
#define ll_min LLONG_MIN
#define ll_max LLONG_MAX
#define endl "\n"
#define F first
#define S second
#define mkpr(x,y)  make_pair(x,y)
using namespace std;
void weza()
{
    std::ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
}

int main()
{
    weza();
    ll n, m, k, b=0, a = 0, c = 0, x = -1, y;
    string s;
    cin >> s;
    cin >> n;
    vector<int>arr(50);
    for (int i = 0; i < 24; i++)
    {
        cin >> x;
        arr[i] = x;
        b = max(b, x);
    }
    x = 0;
    for (int i = 0; i < s.size(); i++)
    {
        x += (i+1) * arr[s[i] - 'a'];
    }
    x +=(( (s.size() + n) * (s.size() + n + 1) / 2)-(s.size()*(s.size()+1)/2))*b;
    cout << x << endl;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<sstream>
#include<string>
#include<algorithm>
#include<math.h>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<list>
#include<set>
#include<map>
#include<bitset>
#include<iterator>
#include <stack>
#define outfile freopen("", "w", stdout);
#define infile freopen("", "r", stdin);
#define ll long long
const ll INF = 2e18;
typedef long double ld;
#define all(v) ((v).begin()),((v).end())
#define vec2(n,m) vector<vector<ll>>vv(n + 10, vector<ll>(m + 10, 0));
#define p_q1   priority_queue<int,vector<int>,greater<int>>pq1;
#define format(n) fixed<<setprecision(n)
#define ll_min LLONG_MIN
#define ll_max LLONG_MAX
#define endl "\n"
#define F first
#define S second
#define mkpr(x,y)  make_pair(x,y)
using namespace std;
void weza()
{
    std::ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
}

int main()
{
    weza();
    ll n, m, k, b, a = 0, c = 0, x = -1, y;
    string s;
    cin >> s;
    cin >> n;
    vector<int>arr(25);
    for (int i = 0; i < 24; i++)
    {
        cin >> x;
        arr[i] = x;
        b = max(b, x);
    }
    x = 0;
    for (int i = 0; i < s.size(); i++)
    {
        x += (i+1) * arr[s[i] - 'a'];
    }
    x +=(( (s.size() + n) * (s.size() + n + 1) / 2)-(s.size()*(s.size()+1)/2))*b;
    cout << x << endl;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<sstream>
#include<string>
#include<algorithm>
#include<math.h>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<list>
#include<set>
#include<map>
#include<bitset>
#include<iterator>
#include <stack>
#define outfile freopen("", "w", stdout);
#define infile freopen("", "r", stdin);
#define ll long long
const ll INF = 2e18;
typedef long double ld;
#define all(v) ((v).begin()),((v).end())
#define vec2(n,m) vector<vector<ll>>vv(n + 10, vector<ll>(m + 10, 0));
#define p_q1   priority_queue<int,vector<int>,greater<int>>pq1;
#define format(n) fixed<<setprecision(n)
#define ll_min LLONG_MIN
#define ll_max LLONG_MAX
#define endl "\n"
#define F first
#define S second
#define mkpr(x,y)  make_pair(x,y)
using namespace std;
void weza()
{
    std::ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
}

int main()
{
    weza();
    ll n, m, k, b=0, a = 0, c = 0, x = -1, y;
    string s;
    cin >> s;
    cin >> n;
    vector<int>arr(50);
    for (int i = 0; i < 26; i++)
    {
        cin >> x;
        arr[i] = x;
        b = max(b, x);
    }
    x = 0;
    for (int i = 0; i < s.size(); i++)
    {
        x += (i+1) * arr[s[i] - 'a'];
    }
   // cout << x << endl;
    x +=(((s.size() + n) * (s.size() + n + 1) / 2)-(s.size()*(s.size()+1)/2))*b;
    cout << x << endl;
}


#include<bits/stdc++.h>
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define forN(i, n) for(int i = 0; i <n ; ++i)
#define endl "\n"
typedef long long ll;
using namespace  std;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const int N = 1e5 + 5;
string s;
int k, W[26];

int main() {
    fastIO;
    cin>>s>>k;
    int mx = 0;
    for(int i = 0 ; i<26 ; i++){
        cin>>W[i];
        mx = max(mx,W[i]);
    }
    int ans = 0;
    for(int i = 0 ; i<s.size() ; i++){
        ans += W[i]*(i+1);
    }
    for(int i = s.size() ; i<s.size()+k ;i++){
        ans += mx*(i+1);
    }
    cout<<ans;
}


#include<bits/stdc++.h>
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define forN(i, n) for(int i = 0; i <n ; ++i)
#define endl "\n"
typedef long long ll;
using namespace  std;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const int N = 1e5 + 5;
string s;
int k, W[26];

int main() {
    fastIO;
    cin>>s>>k;
    int mx = 0;
    for(int i = 0 ; i<26 ; i++){
        cin>>W[i];
        mx = max(mx,W[i]);
    }
    int ans = 0;
    for(int i = 0 ; i<s.size() ; i++){
        ans += W[s[i]-'a']*(i+1);
    }
    for(int i = s.size() ; i<s.size()+k ;i++){
        ans += mx*(i+1);
    }
    cout<<ans;
}


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

string s;
int k;
int values[26];
vector<int> rem ;
int main(){
    cin >> s;
    cin >> k;
    int ans = 0;
    for (int i = 0 ; i<26 ; i++){
        cin>>values[i];
    }
    for (int i = 0 ; i<=s.size();i++){
        ans+=(i+1)*values[s[i]-'a'];
    }
    int m = *max_element(values, values + 26);
    for (int i = s.size() ; i<s.size()+k;i++){
        ans+=(i+1)*m;
    }
    cout<<ans;
}


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

string s;
int k;
int values[26];
vector<int> rem ;
int main(){
    cin >> s;
    cin >> k;
    int ans = 0;
    for (int i = 0 ; i<26 ; i++){
        cin>>values[i];
    }
    for (int i = 0 ; i<=s.size();i++){
        ans+=(i+1)*values[s[i]-'a'];
    }
    sort(values, values + 26, greater<int>());
    int m = values[0];
    for (int i = s.size() ; i<s.size()+k;i++){
        ans+=(i+1)*m;
    }
    cout<<ans;
}


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

string s;
int k;
int values[26];
vector<int> rem ;
int main(){
    cin >> s;
    cin >> k;
    int ans = 0;
    for (int i = 0 ; i<26 ; i++){
        cin>>values[i];
    }
    for (int i = 0 ; i<=s.size();i++){
        ans+=(i+1)*values[s[i]-'a'];
    }
    int m = max_element(values, values + 26);
    for (int i = s.size() ; i<s.size()+k;i++){
        ans+=(i+1)*m;
    }
    cout<<ans;
}


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

int main(){
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);   cout.tie(NULL);
   string s;
   cin >> s;

   double k,x;
   int s_len=s.size(),highest=0;
   cin >> k;

   int arr[50];
   for(int i=1;i<=26;i++){
      cin >> arr[i];
      highest=max(highest,arr[i]);
   }

   ll total=0;
   for(int i=0;i<s_len;i++)
      total+=(i+1)*arr[((int)s[i])-96];
      // huruf ke-n = (int)s[i]-96;
   // a=s_len+1, b=1, n=k;

   x=(k/2)*(s_len+1+s_len+k);
   x*=highest;
   cout << total+x << '\n';
}
/*

*/


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <iomanip>
#include <map>
#include <cstdio>
#include <sstream>
#define ll long long
using namespace std;

int main(){
   ios_base::sync_with_stdio(false);
   string s;
   cin >> s;

   int k;
   cin >> k;

   long double total=0;
   int arr[100];
   for(int i=0;i<26;i++){
      cin >> arr[i];
   }

   for(int i=0;i<s.size();i++){
      total+=(arr[((int) s[i])-97]*(i+1));
   }

   long double len=s.size();
   sort(arr,arr+26);
   long double rand=arr[25];

   long double x=((k+len)-k);
   x/=2;
   x*=(k+1)+(len+k);
   x*=rand;
   x+=total;
   cout << x << '\n';
}
/*
4*2 + 5*2 +6*2
n/2 * (a+un)
1,5 * (10)

*/


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

int main(){
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);   cout.tie(NULL);
   string s;
   cin >> s;

   double k,x;
   double s_len=s.size(),highest=0;
   cin >> k;

   double arr[50];
   for(int i=1;i<=26;i++){
      cin >> arr[i];
      highest=max(highest,arr[i]);
   }

   ll total=0;
   for(int i=0;i<s_len;i++)
      total+=(i+1)*arr[((int)s[i])-96];
      // huruf ke-n = (int)s[i]-96;
   // a=s_len+1, b=1, n=k;

   x=(k/2)*(s_len+1+s_len+k)*highest;
   ll rand=x;
   cout << total+rand << '\n';
}
/*

*/


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long n,m,i,j,k,l,sum=0;
	int maxi=INT_MIN,a[26];
	string s;
	cin>>s;
	cin>>k;
	for(i=1;i<=26;i++)
	{
		cin>>a[i];
		maxi=max(maxi,a[i]);
	}
	for(i=0;i<s.length();i++)
	{
		char c=s[i];
		l=int(c);
		sum+=(i+1)*a[(l%96)];
	}
	for(i=s.length()+1;i<=s.length()+k;i++)
	sum+=i*maxi;
	cout<<sum;
}


#include<bits/stdc++.h>
#include<vector>
using namespace std;
int main()
{
	long long n,m,i,j,k,l,sum=0;
	int maxi=INT_MIN;
	string s;
	cin>>s;
	cin>>k;
	int a[26];
	for(i=1;i<=26;i++)
	{
		cin>>a[i];
		maxi=max(maxi,a[i]);
	}
	for(i=0;i<s.length();i++)
	{
		char c=s[i];
		l=int(c);
		sum+=(i+1)*a[(l%96)];
	}
	for(i=s.length()+1;i<=s.length()+k;i++)
	sum+=i*maxi;
	cout<<sum;
}


#include<bits/stdc++.h>
#include<vector>
using namespace std;
int main()
{
	long long n,m,i,j,k,l,sum=0;
	int maxi=INT_MIN;
	string s;
	cin>>s;
	cin>>k;
	int a[26];
	for(i=1;i<=26;i++)
	{
		cin>>a[i];
		maxi=max(maxi,a[i]);
	}
	for(i=0;i<s.length();i++)
	{
		char c=s[i];
		//cout<<a[(int(c)%96)]<<endl;
		sum+=(i+1)*a[(int(c)%96)];
	}
	//cout<<sum;
	for(i=s.length()+1;i<=s.length()+k;i++)
	sum+=i*maxi;
	cout<<sum;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long n,m,i,j,k,l,sum=0;
	int maxi=INT_MIN,a[26];
	string s;
	cin>>s;
	cin>>k;
	for(i=0;i<26;i++)
	{
		cin>>a[i];
		maxi=max(maxi,a[i]);
	}
	for(i=0;i<s.length();i++)
	sum+=a[s[i]-'a']*(i+1);
	for(i=s.length()+1;i<=s.length()+k;i++)
	sum+=i*maxi;
	cout<<sum;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    string s;
    cin>>s;
    ll k;
    cin>>k;
    ll a[26]={0};
    ll m=INT_MIN,sum=0,pos=-1;
    ch='a';
    for(ll i=0;i<26;i++)
    {
        cin>>a[i];
        if(m<=a[i])
        {
            m=a[i];
            ch=(char)(i+97);
            pos=i;
        }
    }
    for(int i=0;i<k;i++)
    s+=ch;
    for(ll i=0;i<s.length();i++)
        sum+=a[pos]*(i+1);
    cout<<sum<<"\n";
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    string s;
    cin>>s;
    ll k;
    cin>>k;
    ll a[26]={0};
    ll m=INT_MIN,sum=0,pos=-1;
    char ch='a';
    for(ll i=0;i<26;i++)
    {
        cin>>a[i];
        if(m<=a[i])
        {
            m=a[i];
            ch=(char)(i+97);
            pos=i;
        }
    }
    for(int i=0;i<k;i++)
    s+=ch;
    for(ll i=0;i<s.length();i++)
        sum+=a[pos]*(i+1);
    cout<<sum<<"\n";
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    string s;
    cin>>s;
    ll k;
    cin>>k;
    ll a[26]={0};
    ll m=INT_MIN;
    ch='a';
    for(int i=0;i<26;i++)
    {
        cin>>a[i];
        if(m<=a[i])
        {
            m=a[i];
            ch=(char)(i+97);
        }
    }
    for(int i=0;i<k;i++)
    s+=ch;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    string s;
    cin>>s;
    ll k;
    cin>>k;
    ll a[26]={0};
    ll m=INT_MIN,sum=0;
    char ch='a';
    for(ll i=0;i<26;i++)
    {
        cin>>a[i];
        if(m<=a[i])
        {
            m=a[i];
            ch=(char)(i+97);
        }
    }
    for(int i=0;i<k;i++)
    s+=ch;
    for(ll i=0;i<s.length();i++)
        sum+=a[s[i]-97]*(i+1);
    cout<<sum<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int i,j,k,l=0,m,n,p,q;
    string a,b,c;
    int x[27]={0};
    vector <int> v1,v2;
    cin>>a>>k;
    m=a.size();
    for(i=0;i<26;i++){
        cin>>p;
        v1.push_back(p);
    }
    v2=v1;
    for(i=0;i<m;i++){
        k=a[i]-'a';
        x[i]=v1[k];
    }
    for(i=0;i<m;i++){
        l+=(i+1)*x[i];
       // cout<<i+1<<endl;
    }
    sort(v2.begin(),v2.end());
    for(j=m;j<=k+m;j++){
        l+=(j+1)*v2[25];
       // cout<<j+1<<endl;
    }
    cout<<l<<endl;
    return 0;
}