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 query {
  int l, r, id;
  bool operator<(const query& k) const {
    if (l / 330 == k.l / 330)
      return r < k.r;
    else
      return l < k.l;
  }
} t[100005];
int n, s[100005], q, ans[100005];
int ql, qr, tr[100005], cnt[100005], cur = 0, gg = 0;
list<int> L[100005];
void modir(int tarr) {
  if (qr < tarr) {
    for (int i = qr + 1; i <= tarr; i++) {
      if (!cnt[s[i]]) cur++;
      cnt[s[i]]++;
      if (cnt[s[i]] > 1 && tr[L[s[i]].front()] < i &&
          tr[L[s[i]].front()] >= L[s[i]].back())
        gg++;
      L[s[i]].push_back(i);
    }
  } else {
    for (int i = qr - 1; i >= tarr; i--) {
      cnt[s[i + 1]]--;
      if (!cnt[s[i + 1]]) cur--;
      int tmp = L[s[i + 1]].back();
      L[s[i + 1]].pop_back();
      if (cnt[s[i + 1]] > 1 && tr[L[s[i + 1]].front()] < tmp &&
          tr[L[s[i + 1]].front()] >= L[s[i + 1]].back())
        gg--;
    }
  }
  qr = tarr;
  return;
}
void modil(int tarl) {
  if (ql < tarl) {
    for (int i = ql + 1; i <= tarl; i++) {
      cnt[s[i - 1]]--;
      if (!cnt[s[i - 1]]) cur--;
      int tmp = L[s[i - 1]].front();
      L[s[i - 1]].pop_front();
      if (cnt[s[i - 1]] > 1 && tr[tmp] < L[s[i - 1]].back() &&
          tr[L[s[i - 1]].front()] >= L[s[i - 1]].back())
        gg--;
    }
  } else {
    for (int i = ql - 1; i >= tarl; i--) {
      if (!cnt[s[i]]) cur++;
      cnt[s[i]]++;
      if (cnt[s[i]] > 1 && tr[i] < L[s[i]].back() &&
          tr[L[s[i]].front()] >= L[s[i]].back())
        gg++;
      L[s[i]].push_front(i);
    }
  }
  ql = tarl;
  return;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &s[i]);
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d %d", &t[i].l, &t[i].r);
    t[i].id = i;
  }
  sort(t + 1, t + q + 1);
  memset(cnt, 0, sizeof(cnt));
  for (int i = 1; i <= 100000; i++) L[i].push_back(n + 1);
  for (int i = n; i >= 1; i--) {
    if (cnt[s[i]] > 1) {
      int a, b;
      a = L[s[i]].front();
      L[s[i]].pop_front();
      b = L[s[i]].front();
      L[s[i]].push_front(a);
      if (b - a != a - i) {
        while (L[s[i]].back() != b) {
          L[s[i]].pop_back();
          cnt[s[i]]--;
        }
      }
    }
    L[s[i]].push_front(i);
    cnt[s[i]]++;
    tr[i] = L[s[i]].back() - 1;
  }
  for (int i = 1; i <= 100000; i++) {
    while (!L[i].empty()) L[i].pop_front();
  }
  memset(ans, 0, sizeof(ans));
  memset(cnt, 0, sizeof(cnt));
  ql = 1, qr = 0;
  for (int i = 1; i <= q; i++) {
    modir(t[i].r);
    modil(t[i].l);
    ans[t[i].id] = cur;
    if (gg == cur) ans[t[i].id]++;
  }
  for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
  return 0;
}
