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;
struct Tree {
  int sum, minNum;
} tree[100005 << 2];
void pushUp(int rt) {
  tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
  tree[rt].minNum = min(tree[rt << 1].minNum, tree[rt << 1 | 1].minNum);
}
void buildTree(int l, int r, int rt) {
  tree[rt].sum = 0;
  tree[rt].minNum = 100000000;
  if (l == r) return;
  int m = (l + r) >> 1;
  buildTree(l, m, rt << 1);
  buildTree(m + 1, r, rt << 1 | 1);
}
void update(int pos, int add, int minNum, int l, int r, int rt) {
  if (l == r) {
    tree[rt].sum += add;
    tree[rt].minNum = minNum;
    return;
  }
  int m = (l + r) >> 1;
  if (pos <= m)
    update(pos, add, minNum, l, m, rt << 1);
  else
    update(pos, add, minNum, m + 1, r, rt << 1 | 1);
  pushUp(rt);
}
int querySum(int ll, int rr, int l, int r, int rt) {
  if (ll > rr) return 0;
  if (ll <= l && r <= rr) return tree[rt].sum;
  int m = (l + r) >> 1, res = 0;
  if (ll <= m) res += querySum(ll, rr, l, m, rt << 1);
  if (rr > m) res += querySum(ll, rr, m + 1, r, rt << 1 | 1);
  return res;
}
int queryMinNum(int ll, int rr, int l, int r, int rt) {
  if (ll > rr) return 100000000;
  if (ll <= l && r <= rr) return tree[rt].minNum;
  int m = (l + r) >> 1, res = 100000000;
  if (ll <= m) res = min(res, queryMinNum(ll, rr, l, m, rt << 1));
  if (rr > m) res = min(res, queryMinNum(ll, rr, m + 1, r, rt << 1 | 1));
  return res;
}
struct Ask {
  int l, r, id;
  bool operator<(const Ask& x) const { return r < x.r; }
} ask[100005];
int star[100005], last[100005], num[100005];
int ans[100005], L[100005], lastLast[100005];
int n, q;
int main() {
  while (scanf("%d", &n) != EOF) {
    for (int i = 1; i <= n; i++) scanf("%d", &star[i]);
    scanf("%d", &q);
    for (int l, r, i = 0; i < q; i++) {
      scanf("%d%d", &ask[i].l, &ask[i].r);
      ask[i].id = i;
    }
    sort(ask, ask + q);
    for (int i = 1; i <= 100000; i++) last[i] = 0, lastLast[i] = 0;
    int now = 0;
    for (int i = 1; i <= n && now < q; i++) {
      if (last[star[i]] == 0) {
        num[star[i]] = 0;
        L[star[i]] = 0;
        last[star[i]] = i;
        update(last[star[i]], 1, L[star[i]], 1, n, 1);
      } else {
        if (i - last[star[i]] != num[star[i]]) {
          L[star[i]] = lastLast[star[i]];
          num[star[i]] = i - last[star[i]];
        }
        update(last[star[i]], -1, 100000000, 1, n, 1);
        lastLast[star[i]] = last[star[i]] + 1;
        last[star[i]] = i;
        update(last[star[i]], 1, L[star[i]], 1, n, 1);
      }
      while (now < q && ask[now].r == i) {
        ans[ask[now].id] = querySum(ask[now].l, ask[now].r, 1, n, 1) + 1;
        if (ask[now].l >= queryMinNum(ask[now].l, ask[now].r, 1, n, 1))
          ans[ask[now].id]--;
        now++;
      }
    }
    for (int i = 0; i < q; i++) printf("%d\n", ans[i]);
  }
  return 0;
}
