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 MN = 100000, SQ = 317;
struct query {
  int a, b;
};
int n, t;
int a[MN], pre[MN], ne[MN], tab[MN];
int snum[MN];
query qu[MN];
vector<int> divi[SQ];
bool mocmp(int a, int b) {
  if (qu[a].a == qu[b].a) {
    return a < b;
  }
  return qu[a].a < qu[b].a;
}
void div_qu() {
  for (int i = 0; i < (t); i++) {
    divi[qu[i].b / SQ].push_back(i);
  }
  for (int i = 0; i < (SQ); i++) {
    sort(divi[i].begin(), divi[i].end(), mocmp);
  }
}
void pre_ne() {
  for (int i = 0; i < (MN); i++) tab[i] = -1;
  for (int i = 0; i < (n); i++) {
    pre[i] = tab[a[i]];
    tab[a[i]] = i;
  }
  for (int i = 0; i < (MN); i++) tab[i] = -1;
  for (int i = (n - 1); i >= (0); i--) {
    ne[i] = tab[a[i]];
    tab[a[i]] = i;
  }
}
void divide_into_substrings() {
  for (int i = 0; i < (n); i++) {
    if (pre[i] != -1 and pre[pre[i]] != -1 and
        i - pre[i] == pre[i] - pre[pre[i]]) {
      snum[i] = snum[pre[i]];
    } else if (pre[i] != -1) {
      snum[i] = snum[pre[i]] + 1;
    } else {
      snum[i] = 0;
    }
  }
}
struct {
  int first[MN], last[MN], cnt;
  bool seq[MN];
  int b, e, cnt_has;
  void pushb() {
    int x = a[++e];
    if (last[x] == -1) {
      first[x] = last[x] = e;
      seq[x] = true;
      cnt_has++;
      cnt++;
      return;
    }
    if (seq[x] && snum[ne[first[x]]] != snum[e]) {
      seq[x] = false;
      cnt_has--;
    }
    last[x] = e;
  }
  void popb() {
    int x = a[e];
    if (first[x] == e) {
      first[x] = last[x] = -1;
      seq[x] = false;
      cnt_has--;
      cnt--;
      e--;
      return;
    }
    last[x] = pre[e];
    if (!seq[x] &&
        (first[x] == last[x] || snum[ne[first[x]]] == snum[last[x]])) {
      seq[x] = true;
      cnt_has++;
    }
    e--;
  }
  void pushf() {
    int x = a[--b];
    if (first[x] == -1) {
      first[x] = last[x] = b;
      seq[x] = true;
      cnt_has++;
      cnt++;
      return;
    }
    if (seq[x] && snum[last[x]] != snum[ne[b]]) {
      seq[x] = false;
      cnt_has--;
    }
    first[x] = b;
  }
  void popf() {
    int x = a[b];
    if (last[x] == b) {
      first[x] = last[x] = -1;
      seq[x] = false;
      cnt_has--;
      cnt--;
      b++;
      return;
    }
    first[x] = ne[b];
    if (!seq[x] &&
        (first[x] == last[x] || snum[ne[first[x]]] == snum[last[x]])) {
      seq[x] = true;
      cnt_has++;
    }
    b++;
  }
  void init(int _b, int _e) {
    for (int i = 0; i < (MN); i++) first[i] = last[i] = -1;
    for (int i = 0; i < (MN); i++) seq[i] = false;
    cnt = cnt_has = 0;
    b = _b;
    e = _b - 1;
    while (e < _e) pushb();
  }
  int res() { return cnt + (cnt_has ? 0 : 1); }
} mo;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < (n); i++) {
    cin >> a[i];
    a[i]--;
  }
  pre_ne();
  divide_into_substrings();
  cin >> t;
  for (int i = 0; i < (t); i++) {
    int a, b;
    cin >> a >> b;
    qu[i].a = a - 1;
    qu[i].b = b - 1;
  }
  div_qu();
  bool first = true;
  for (int j = 0; j < (SQ); j++) {
    for (int i : divi[j]) {
      if (first) {
        mo.init(qu[i].a, qu[i].b);
        first = false;
      } else {
        while (mo.e < qu[i].b) mo.pushb();
        while (mo.b > qu[i].a) mo.pushf();
        while (mo.e > qu[i].b) mo.popb();
        while (mo.b < qu[i].a) mo.popf();
      }
      tab[i] = mo.res();
    }
  }
  for (int i = 0; i < (t); i++) cout << tab[i] << '\n';
}
