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("O3")
const double PI = acos(-1);
using namespace std;
const int MAX = 100 * 1000 + 10;
int A[MAX], prv[MAX], lst[MAX], n;
vector<int> ST[MAX * 4];
int T[2][MAX];
int L[MAX], R[MAX], ans[MAX];
vector<int> Q[MAX];
void update(int p, int v, int *BIT) {
  for (; p <= n; p += ((p) & (-(p)))) BIT[p] += v;
}
int query(int p, int *BIT) {
  int ret = 0;
  for (; p; p ^= ((p) & (-(p)))) ret += BIT[p];
  return ret;
}
void merge(vector<int> &A, vector<int> &B, vector<int> &C) {
  int i = 0, j = 0;
  C.reserve(((int)A.size()) + ((int)B.size()));
  while (i < ((int)A.size()) && j < ((int)B.size())) {
    if (A[i] < B[j])
      C.push_back(A[i++]);
    else
      C.push_back(B[j++]);
  }
  while (i < ((int)A.size())) C.push_back(A[i++]);
  while (j < ((int)B.size())) C.push_back(B[j++]);
}
void build(int node, int s, int e) {
  if (s == e) {
    ST[node].push_back(prv[s]);
    return;
  }
  int m = (s + e) >> 1, left = node << 1, right = left | 1;
  build(left, s, m);
  build(right, m + 1, e);
  merge(ST[left], ST[right], ST[node]);
}
int query(int node, int s, int e, int l, int r, int th) {
  if (l <= s && e <= r)
    return upper_bound(ST[node].begin(), ST[node].end(), th) - ST[node].begin();
  int m = (s + e) >> 1, left = node << 1, right = left | 1;
  if (r <= m) return query(left, s, m, l, r, th);
  if (m < l) return query(right, m + 1, e, l, r, th);
  return query(left, s, m, l, m, th) + query(right, m + 1, e, m + 1, r, th);
}
int main() {
  scanf("%d", &n);
  for (int i = (1); i <= (n); i++) scanf("%d", A + i);
  for (int i = (1); i <= (n); i++) {
    int x = A[i];
    prv[i] = lst[x];
    lst[x] = i;
  }
  build(1, 1, n);
  int m;
  scanf("%d", &m);
  for (int i = 0; i < (m); i++) {
    scanf("%d %d", L + i, R + i);
    ans[i] = query(1, 1, n, L[i], R[i], L[i] - 1) + 1;
    Q[R[i]].push_back(i);
  }
  for (int r = 1; r <= n; r++) {
    int b = prv[r], a = prv[b], x = A[r];
    if (b == 0) {
      update(1, 1, T[0]);
      update(r, 1, T[1]);
      lst[x] = 1;
    } else if (a != 0 && b - a != r - b) {
      update(lst[x], -1, T[0]);
      update(a + 1, 1, T[0]);
      lst[x] = a + 1;
      update(b, -1, T[1]);
      update(r, 1, T[1]);
    } else {
      update(b, -1, T[1]);
      update(r, 1, T[1]);
    }
    for (int q : Q[r]) {
      int A = query(L[q], T[0]), B = query(L[q] - 1, T[1]), tmp = A - B;
      assert(tmp >= 0);
      ans[q] -= tmp > 0;
    }
  }
  for (int i = 0; i < (m); i++) printf("%d\n", ans[i]);
  return 0;
}
