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 = 100010;
int lowbits(int n) { return n & (-n); }
void insert(int p, int c, int arr[]) {
  while (p < N) {
    arr[p] += c;
    p += lowbits(p);
  }
}
int query(int p, int arr[]) {
  int ret = 0;
  while (p) {
    ret += arr[p];
    p -= lowbits(p);
  }
  return ret;
}
int n, q, arr[N], last[N], dif_num[N], add[N];
int bit[N];
int last1[N], last2[N], gap[N], shift[N];
pair<int, pair<int, int> > Q[N];
bool cmp(pair<int, pair<int, int> > a, pair<int, pair<int, int> > b) {
  if (a.second.second != b.second.second)
    return a.second.second < b.second.second;
  return a.second.first < b.second.first;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> arr[i];
  cin >> q;
  for (int i = 0; i < q; i++) Q[i].first = i;
  for (int i = 0; i < q; i++) cin >> Q[i].second.first >> Q[i].second.second;
  sort(Q, Q + q, cmp);
  int point = 0;
  for (int i = 1; i <= n; i++) {
    if (last[arr[i]]) {
      insert(last[arr[i]], -1, bit);
    }
    last[arr[i]] = i;
    insert(i, 1, bit);
    while (point < q && Q[point].second.second <= i) {
      dif_num[Q[point].first] = query(Q[point].second.second, bit) -
                                query(Q[point].second.first - 1, bit);
      point++;
    }
  }
  memset(bit, 0, sizeof bit);
  memset(last, 0, sizeof last);
  point = 0;
  for (int i = 1; i <= n; i++) {
    int v = arr[i];
    if (last1[v]) {
      if (gap[v]) {
        if (i - last1[v] == gap[v]) {
        } else {
          int pre = last1[v] - gap[v];
          insert(pre, -1, bit);
          if (shift[v]) insert(shift[v], 1, bit);
          shift[v] = pre;
          gap[v] = i - last1[v];
          last2[v] = last1[v];
        }
      } else {
        gap[v] = i - last1[v];
        last2[v] = last1[v];
      }
    } else {
    }
    last1[v] = i;
    while (point < q && Q[point].second.second <= i) {
      add[Q[point].first] = query(Q[point].second.second, bit) -
                            query(Q[point].second.first - 1, bit);
      point++;
    }
  }
  for (int i = 0; i < q; i++) {
    int _put = dif_num[i];
    if (dif_num[i] + add[i] == 0) _put++;
    cout << _put << endl;
  }
}
