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;
int n, Q;
int a[100100];
int ans[100100];
int last[100100], add[4 * 100100];
struct str {
  int l, r, id;
  str(int l = 0, int r = 0, int id = 0) : l(l), r(r), id(id){};
} q[100100];
bool operator<(str a, str b) { return a.r != b.r ? a.r < b.r : a.l < b.l; }
void input() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", a + i);
  scanf("%d", &Q);
  for (int i = 1; i <= Q; i++) scanf("%d %d", &q[i].l, &q[i].r), q[i].id = i;
  sort(q + 1, q + Q + 1);
}
void push_down(int id) {
  if (add[id]) {
    add[2 * id + 1] += add[id], add[2 * id] += add[id];
    add[id] = 0;
  }
}
void insert(int id, int st, int en, int a, int b, int v) {
  if (a <= st && en <= b) {
    add[id] += v;
    return;
  }
  push_down(id);
  int mid = (st + en) / 2;
  if (mid >= a) insert(2 * id, st, mid, a, b, v);
  if (b >= mid + 1) insert(2 * id + 1, mid + 1, en, a, b, v);
}
int calc(int id, int st, int en, int a) {
  if (st == a && en == a) {
    return add[id];
  }
  push_down(id);
  int mid = (st + en) / 2;
  if (mid >= a)
    return calc(2 * id, st, mid, a);
  else
    return calc(2 * id + 1, mid + 1, en, a);
}
void distinguish() {
  memset(add, 0, sizeof(add));
  memset(last, 0, sizeof(last));
  int cur = 1;
  for (int i = 1; i <= n; i++) {
    insert(1, 1, n, last[a[i]] + 1, i, 1);
    last[a[i]] = i;
    while (cur <= Q && q[cur].r == i) {
      ans[q[cur].id] = calc(1, 1, n, q[cur].l);
      cur++;
    }
  }
}
int gas[100100];
int pre[100100];
int d[100100];
void Insert(int id, int st, int en, int a, int b, int v) {}
void Calc(int id, int st, int en, int a, int b) {}
void same() {
  memset(add, 0, sizeof(add));
  memset(d, 0, sizeof(d));
  memset(gas, 0, sizeof(gas));
  memset(last, 0, sizeof(last));
  int cur = 1;
  for (int i = 1; i <= n; i++) {
    int x = a[i];
    int j = last[x];
    pre[i] = last[x];
    last[x] = i;
    if (!gas[x]) {
      insert(1, 1, n, 1, i, 1);
    } else {
      insert(1, 1, n, j + 1, i, 1);
      if (!d[x] || d[x] == i - pre[i]) {
        ;
      } else {
        int jj = j, k;
        while (jj) {
          k = pre[jj];
          if (!k || jj - k != d[x]) break;
          jj = k;
        }
        insert(1, 1, n, k + 1, pre[j], -1);
      }
      d[x] = i - pre[i];
    }
    gas[x]++;
    while (cur <= Q && q[cur].r == i) {
      ans[q[cur].id] += (calc(1, 1, n, q[cur].l) == 0);
      cur++;
    }
  }
}
void print() {
  for (int i = 1; i <= Q; i++) printf("%d\n", ans[i]);
}
int main() {
  input();
  distinguish();
  same();
  print();
  return 0;
}
