Cosider a sequence, consisting of n integers: a1, a2, ..., an. Jeff can perform the following operation on sequence a:

  * take three integers v, t, k (1 ≤ v, t ≤ n; 0 ≤ k; v + tk ≤ n), such that av = av + t, av + t = av + 2t, ..., av + t(k - 1) = av + tk; 
  * remove elements av, av + t, ..., av + t·k from the sequence a, the remaining elements should be reindexed a1, a2, ..., an - k - 1. 
  * permute in some order the remaining elements of sequence a. 



A beauty of a sequence a is the minimum number of operations that is needed to delete all elements from sequence a.

Jeff's written down a sequence of m integers b1, b2, ..., bm. Now he wants to ask q questions. Each question can be described with two integers li, ri. The answer to the question is the beauty of sequence bli, bli + 1, ..., bri. You are given the sequence b and all questions. Help Jeff, answer all his questions.

Input

The first line contains integer m (1 ≤ m ≤ 105). The next line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 105). 

The third line contains integer q (1 ≤ q ≤ 105) — the number of questions. The next q lines contain pairs of integers, i-th of them contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ m) — the description of i-th question.

Output

In q lines print the answers to Jeff's queries. Print the answers according to the order of questions in input.

Examples

Input

5
2 2 1 1 2
5
1 5
1 1
2 2
1 3
2 3


Output

2
1
1
2
2


Input

10
2 1 3 3 3 3 1 3 1 1
10
4 8
2 10
1 10
4 4
1 3
2 4
6 7
1 9
2 5
1 1


Output

2
3
3
1
3
2
2
3
2
1
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.2")
using namespace std;
inline void opting() {
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
}
long long inp() {
  long long x;
  cin >> x;
  return x;
}
void make_unique(vector<long long> &vec, bool sorted = false) {
  if (!sorted) sort((vec).begin(), (vec).end());
  vec.resize(unique((vec).begin(), (vec).end()) - vec.begin());
}
void file_edit(bool reader = true, bool writer = false) {
  if (reader) freopen("Test_input.txt", "r", stdin);
  if (writer) freopen("Test_output.txt", "w", stdout);
}
const long long SQRT = 65, lg = 18 + 2, inlim = 2147483647;
const long long maxn = 1e5 + 20, delta = 1e9 + 7, inf = 1e9 + 420;
int uni_fen[maxn], sng_fen[maxn];
inline void add_uni(int idx, int val) {
  idx += 10;
  for (; idx < maxn; idx += idx & -idx) uni_fen[idx] += val;
}
inline int get_uni(int idx) {
  idx += 10;
  int sm = 0;
  for (; idx; idx -= idx & -idx) sm += uni_fen[idx];
  return sm;
}
inline void add_sng(int idx, int val) {
  idx += 10;
  for (; idx < maxn; idx += idx & -idx) sng_fen[idx] += val;
}
inline int get_sng(int idx) {
  idx += 10;
  int sm = 0;
  for (; idx; idx -= idx & -idx) sm += sng_fen[idx];
  return sm;
}
struct query {
  int id;
  int L, R;
  bool single = false;
  int cnt;
};
bool L_sort_comp(query A, query B) { return A.L > B.L; }
vector<query> q;
vector<int> pos[maxn], b;
inline void input() {
  int n = inp();
  for (int i = 0; i < n; i++) {
    int tmp = inp();
    b.push_back(tmp);
    pos[tmp].push_back(i);
  }
  int m = inp();
  query tmp;
  tmp.single = false;
  tmp.cnt = 0;
  for (int i = 0; i < m; i++) {
    tmp.id = i;
    tmp.L = inp() - 1;
    tmp.R = inp() - 1;
    q.push_back(tmp);
  }
  sort((q).begin(), (q).end(), L_sort_comp);
}
int last[maxn], jump[maxn], aftr[maxn];
inline void find_last(int id) {
  int v = b[id];
  int nxt = upper_bound((pos[v]).begin(), (pos[v]).end(), id) - pos[v].begin();
  int fnl = pos[v][pos[v].size() - 1];
  if (nxt == pos[v].size()) {
    jump[id] = last[id] = aftr[id] = inf;
    return;
  }
  nxt = pos[v][nxt];
  jump[id] = nxt - id;
  aftr[id] = nxt;
  if (nxt == fnl or jump[nxt] == jump[id])
    last[id] = last[nxt];
  else
    last[id] = aftr[nxt];
}
inline void predo() {
  for (int i = b.size() - 1; i > -1; i--) find_last(i);
}
inline void step(int id) {
  add_uni(id, 1);
  add_uni(aftr[id], -1);
  add_sng(aftr[id], -1);
  if (aftr[id] < maxn) add_sng(last[aftr[id]], 1);
  add_sng(id, 1);
  add_sng(last[id], -1);
}
int ans[maxn];
inline void solve() {
  int Q_cnt = 0;
  for (int i = b.size() - 1; i > -1; i--) {
    step(i);
    while (Q_cnt < q.size() and q[Q_cnt].L == i) {
      q[Q_cnt].cnt = get_uni(q[Q_cnt].R);
      q[Q_cnt].single = (get_sng(q[Q_cnt].R) > 0);
      Q_cnt++;
    }
  }
  for (auto Q : q) ans[Q.id] = Q.cnt + !Q.single;
}
void anses() {
  for (int i = 0; i < q.size(); i++) cout << ans[i] << '\n';
}
int main() {
  opting();
  input();
  predo();
  solve();
  anses();
}
