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 maxn = 1e5 + 50;
const int mod = 1e9 + 7;
const int M = 1e5 + 10;
int n, q, a[maxn], last[maxn], twolast[maxn], d[maxn], A[maxn], B[maxn],
    tot[maxn], seg[4 * maxn], pre[maxn], nex[maxn], ted[maxn];
pair<pair<int, int>, int> p[maxn];
pair<int, int> rel[maxn];
long long get(int id, int L, int R, int l, int r) {
  if (L == l and R == r) return seg[id];
  int mid = (L + R) / 2;
  long long ret = 0;
  if (mid > l) ret += get(2 * id + 0, L, mid, l, min(mid, r));
  if (mid < r) ret += get(2 * id + 1, mid, R, max(l, mid), r);
  return ret;
}
void change(int id, int L, int R, int idx, int val) {
  seg[id] += val;
  if (L + 1 == R) return;
  int mid = (L + R) / 2;
  if (mid > idx)
    change(2 * id + 0, L, mid, idx, val);
  else
    change(2 * id + 1, mid, R, idx, val);
}
bool cmp1(pair<pair<int, int>, int> p1, pair<pair<int, int>, int> p2) {
  if (p1.first.second < p2.first.second) return 1;
  if (p1.first.second == p2.first.second and p1.first.first < p2.first.first)
    return 1;
  return 0;
}
bool cmp(pair<int, int> p1, pair<int, int> p2) {
  return (p1.second < p2.second) or
         (p1.second == p2.second and p1.first < p2.first);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> p[i].first.first >> p[i].first.second;
    p[i].second = i;
  }
  for (int i = 1; i <= n; i++) {
    if (last[a[i]] == 0) {
      last[a[i]] = i;
      rel[i].first = 1;
    } else if (d[a[i]] == 0) {
      twolast[a[i]] = last[a[i]];
      rel[last[a[i]]].second = i;
      d[a[i]] = i - last[a[i]];
      last[a[i]] = i;
      rel[i].first = 1;
    } else {
      if (i - last[a[i]] == d[a[i]]) {
        rel[i].first = rel[last[a[i]]].first;
        rel[last[a[i]]].second = i;
        twolast[a[i]] = last[a[i]];
        last[a[i]] = i;
      } else {
        rel[i].first = twolast[a[i]] + 1;
        d[a[i]] = i - last[a[i]];
        rel[last[a[i]]].second = i;
        twolast[a[i]] = last[a[i]];
        last[a[i]] = i;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (rel[i].second == 0) {
      rel[i].second = n + 1;
    }
    pre[i] = rel[i].first;
    nex[i] = rel[i].second;
  }
  sort(p + 1, p + q + 1);
  sort(rel + 1, rel + n + 1);
  for (int i = 1; i <= n; i++) change(1, 1, M, nex[i], 1);
  int idx = 1;
  for (int i = 1; i <= q; i++) {
    while (idx < p[i].first.first) {
      change(1, 1, M, nex[idx], -1);
      idx++;
    }
    ted[p[i].second] =
        get(1, 1, M, p[i].first.second + 1, M) - (n - p[i].first.second);
  }
  idx = 1;
  memset(seg, 0, sizeof seg);
  for (int i = 1; i <= q; i++) {
    while (rel[idx].first <= p[i].first.first and idx <= n) {
      change(1, 1, M, rel[idx].second, 1);
      idx++;
    }
    tot[p[i].second] = get(1, 1, M, p[i].first.second + 1, M);
  }
  memset(seg, 0, sizeof seg);
  idx = 1;
  int idx1 = 1;
  for (int i = 1; i <= q; i++) {
    while (rel[idx].first <= p[i].first.first and idx <= n) {
      change(1, 1, M, rel[idx].second, 1);
      idx++;
    }
    while (idx1 < p[i].first.first) {
      change(1, 1, M, nex[idx1], -1);
      idx1++;
    }
    A[p[i].second] = get(1, 1, M, p[i].first.second + 1, M);
  }
  memset(seg, 0, sizeof seg);
  sort(p + 1, p + q + 1, cmp1);
  sort(rel + 1, rel + n + 1, cmp);
  idx = n, idx1 = n;
  for (int i = q; i >= 1; i--) {
    while (rel[idx].second > p[i].first.second and idx >= 1) {
      change(1, 1, M, rel[idx].first, 1);
      idx--;
    }
    while (idx1 > p[i].first.second) {
      change(1, 1, M, pre[idx1], -1);
      idx1--;
    }
    B[p[i].second] = get(1, 1, M, 1, p[i].first.first + 1);
  }
  for (int i = 1; i <= q; i++) cout << ted[i] + (tot[i] == A[i] + B[i]) << endl;
}
