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>
using namespace std;
const int K = 400, N = 1e5 + 10;
struct query {
  int l, r, ind, res;
  bool operator<(const query &o) const {
    int b1 = l / K, b2 = o.l / K;
    if (b1 != b2) return b1 < b2;
    return r < o.r;
  }
};
int n, a[N], d[N], ngood = 0;
int last[N], dif[N], jump[N], cnt[N], distinct = 0;
query q[N];
bool good[N];
deque<int> prog[N];
void check(int x) {
  deque<int> &q = prog[x];
  bool v;
  if (q.empty())
    v = false;
  else if (((int)(q).size()) == 1)
    v = true;
  else
    v = q.front() > jump[q.back()];
  if (good[x] && !v) ngood--;
  if (!good[x] && v) ngood++;
  good[x] = v;
}
void add(int x, int delta) {
  if (cnt[x] == 0) distinct++;
  cnt[x] += delta;
  if (cnt[x] == 0) distinct--;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < (int)(n); i++) cin >> a[i];
  fill(last, last + N, -1);
  fill(dif, dif + N, -1);
  for (int i = 0; i < (int)(n); i++) {
    int x = a[i];
    if (last[x] != -1) {
      int d = i - last[x];
      if (dif[x] != -1 && dif[x] != d) {
        jump[i] = last[x] - dif[x];
      } else
        jump[i] = jump[last[x]];
      dif[x] = d;
    } else
      jump[i] = -1;
    last[x] = i;
  }
  int m;
  cin >> m;
  for (int i = 0; i < (int)(m); i++) {
    cin >> q[i].l >> q[i].r;
    q[i].l--;
    q[i].ind = i;
  }
  sort(q, q + m);
  int cl = 0, cr = 0;
  for (int i = 0; i < (int)(m); i++) {
    int l = q[i].l, r = q[i].r;
    while (cl > l) {
      --cl;
      prog[a[cl]].push_front(cl);
      check(a[cl]);
      add(a[cl], +1);
    }
    while (cr < r) {
      prog[a[cr]].push_back(cr);
      check(a[cr]);
      add(a[cr], +1);
      cr++;
    }
    while (cl < l) {
      prog[a[cl]].pop_front();
      check(a[cl]);
      add(a[cl], -1);
      cl++;
    }
    while (cr > r) {
      --cr;
      prog[a[cr]].pop_back();
      check(a[cr]);
      add(a[cr], -1);
    }
    int res = distinct;
    check(1);
    if (ngood == 0) res++;
    q[i].res = res;
  }
  sort(q, q + m, [](const query &a, const query &b) { return a.ind < b.ind; });
  for (int i = 0; i < (int)(m); i++) cout << q[i].res << '\n';
  return 0;
}
