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 = 1000 * 100 + 10;
int n, a[N], nxt[N], last[N], ans[N], seg[N << 2], mx[N], cnt[N];
vector<int> vec[N];
pair<int, int> lst[N];
struct Query {
  int id, l, r;
  Query(int s = 0, int e = 0, int d = 0) { l = s, r = e, id = d; }
  bool operator<(const Query &x) const { return r > x.r; }
};
int get(int s, int e, int l = 0, int r = n, int id = 1) {
  if (s <= l && e >= r) return seg[id];
  if (s >= r || e <= l) return -1;
  int mid = l + r >> 1;
  return max(get(s, e, l, mid, id << 1), get(s, e, mid, r, id << 1 | 1));
}
void add(int p, int w, int l = 0, int r = n, int id = 1) {
  if (l + 1 == r) {
    seg[id] = (w == +1 ? mx[l] : -1);
    return;
  }
  int mid = l + r >> 1;
  (p < mid ? add(p, w, l, mid, id << 1) : add(p, w, mid, r, id << 1 | 1));
  seg[id] = max(seg[id << 1], seg[id << 1 | 1]);
}
struct FenwickTree {
  int fen[N];
  FenwickTree() { fill(fen, fen + N, 0); }
  void upd(int x, int w) {
    for (++x; x < N; x += x & (-x)) fen[x] += w;
  }
  int get(int x) {
    int res = 0;
    if (x == -1) return 0;
    for (++x; x; x -= x & (-x)) res += fen[x];
    return res;
  }
  int get(int l, int r) { return get(r - 1) - get(l - 1); }
} T;
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  fill(last, last + N, n);
  for (int i = n - 1; i >= 0; i--) {
    nxt[i] = last[a[i]];
    last[a[i]] = i;
    vec[nxt[i]].push_back(i);
  }
  int Q;
  cin >> Q;
  vector<Query> query;
  for (int i = 0, l, r; i < Q; i++) {
    cin >> l >> r;
    --l;
    query.push_back(Query(l, r, i));
  }
  sort(query.begin(), query.end());
  int r = n;
  for (auto q : query) {
    while (r >= q.r) {
      for (auto p : vec[r]) T.upd(p, +1);
      r--;
    }
    ans[q.id] = T.get(q.l, q.r);
  }
  r = n;
  fill(last, last + N, n);
  for (int i = 0; i < n; i++) vec[i].clear();
  for (int i = n - 1; i >= 0; i--) {
    cnt[a[i]]++;
    if (last[a[i]] < n) {
      int d = last[a[i]] - i;
      if (lst[a[i]].first == d)
        mx[i] = mx[lst[a[i]].second];
      else {
        if (cnt[a[i]] <= 2)
          mx[i] = n;
        else
          mx[i] = nxt[nxt[i]] - 1;
      }
      lst[a[i]] = {d, i};
    } else
      mx[i] = n;
    last[a[i]] = i;
  }
  sort(query.begin(), query.end(), [](Query x, Query y) { return x.l > y.l; });
  r = n - 1;
  fill(cnt, cnt + N, 0);
  for (auto q : query) {
    while (r >= q.l) {
      int i = r;
      if (nxt[i] == n)
        add(i, +1);
      else {
        add(nxt[i], -1);
        add(i, +1);
      }
      r--;
    }
    if (get(q.l, q.r) + 1 < q.r) ans[q.id]++;
  }
  for (int i = 0; i < Q; i++) cout << ans[i] << '\n';
  cout << '\n';
  return 0;
}
