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 N = 1E5 + 5, Q = 1E5 + 5, MX = 1E5 + 5, INF = 1E9 + 7;
int n, q, l, r, a[N], ans[Q], lst[MX];
int nxt[N], pt[N], dif[N], upd[N];
vector<pair<int, int>> que[N];
struct segment_tree {
  int mi[4 * N], su[4 * N];
  void build(int l, int r, int i) {
    mi[i] = INF;
    if (l == r) {
      return;
    }
    build(l, (l + r) / 2, i * 2);
    build((l + r) / 2 + 1, r, i * 2 + 1);
  }
  void update(int l, int r, int i, int u, int v) {
    if (u == 0) {
      return;
    }
    if (l == r) {
      mi[i] = v;
      su[i] = (v == INF ? 0 : 1);
    } else {
      if (u <= (l + r) / 2) {
        update(l, (l + r) / 2, i * 2, u, v);
      } else {
        update((l + r) / 2 + 1, r, i * 2 + 1, u, v);
      }
      mi[i] = min(mi[i * 2], mi[i * 2 + 1]);
      su[i] = su[i * 2] + su[i * 2 + 1];
    }
  }
  int query_min(int l, int r, int i, int L, int R) {
    if (l > R || r < L || L > R) {
      return INF;
    } else if (L <= l && r <= R) {
      return mi[i];
    } else {
      return min(query_min(l, (l + r) / 2, i * 2, L, R),
                 query_min((l + r) / 2 + 1, r, i * 2 + 1, L, R));
    }
  }
  int query_sum(int l, int r, int i, int L, int R) {
    if (l > R || r < L || L > R) {
      return 0;
    } else if (L <= l && r <= R) {
      return su[i];
    } else {
      return query_sum(l, (l + r) / 2, i * 2, L, R) +
             query_sum((l + r) / 2 + 1, r, i * 2 + 1, L, R);
    }
  }
} seg;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    nxt[i] = n + 1;
    nxt[lst[a[i]]] = i;
    upd[i] = lst[a[i]];
    lst[a[i]] = i;
  }
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> l >> r;
    que[r].push_back({l, i});
  }
  seg.build(1, n, 1);
  for (int i = 1; i <= n; i++) {
    if (dif[a[i]] != i - upd[i]) {
      pt[a[i]] = upd[upd[i]];
      dif[a[i]] = i - upd[i];
    }
    int val = pt[a[i]];
    seg.update(1, n, 1, i, val);
    seg.update(1, n, 1, upd[i], INF);
    for (pair<int, int> &v : que[i]) {
      int cur = seg.query_sum(1, n, 1, v.first, i);
      if (seg.query_min(1, n, 1, v.first, i) >= v.first) {
        cur++;
      }
      ans[v.second] = cur;
    }
  }
  for (int i = 1; i <= q; i++) {
    cout << ans[i] << '\n';
  }
}
