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 Pyt {
  int l, r, nr;
};
const int BLOK = 300;
vector<deque<int> > C(100000);
vector<int> zleTr(100000, 0);
vector<int> il(100000, 0);
int zleCiagi = 0, rozne = 0;
void DodajLewo(int x, int a) {
  if (C[x].size() >= 2) {
    int ost = C[x].back();
    C[x].pop_back();
    int pop = C[x].back();
    if (a - ost != ost - pop) {
      ++zleTr[x];
      if (zleTr[x] == 1) ++zleCiagi;
    }
    C[x].push_back(ost);
  }
  C[x].push_back(a);
  ++il[x];
  if (il[x] == 1) ++rozne;
}
void DodajPrawo(int x, int a) {
  if (C[x].size() >= 2) {
    int ost = C[x].front();
    C[x].pop_front();
    int pop = C[x].front();
    if (a - ost != ost - pop) {
      ++zleTr[x];
      if (zleTr[x] == 1) ++zleCiagi;
    }
    C[x].push_front(ost);
  }
  C[x].push_front(a);
  ++il[x];
  if (il[x] == 1) ++rozne;
}
void UsunLewo(int x, int a) {
  if (C[x].size() >= 3) {
    C[x].pop_back();
    int ost = C[x].back();
    C[x].pop_back();
    int pop = C[x].back();
    if (a - ost != ost - pop) {
      --zleTr[x];
      if (zleTr[x] == 0) --zleCiagi;
    }
    C[x].push_back(ost);
  } else
    C[x].pop_back();
  --il[x];
  if (il[x] == 0) --rozne;
}
void UsunPrawo(int x, int a) {
  if (C[x].size() >= 3) {
    C[x].pop_front();
    int ost = C[x].front();
    C[x].pop_front();
    int pop = C[x].front();
    if (a - ost != ost - pop) {
      --zleTr[x];
      if (zleTr[x] == 0) --zleCiagi;
    }
    C[x].push_front(ost);
  } else
    C[x].pop_front();
  --il[x];
  if (il[x] == 0) --rozne;
}
bool por(Pyt a, Pyt b) {
  if (a.l / BLOK < b.l / BLOK) return true;
  if (a.l / BLOK > b.l / BLOK) return false;
  if (a.r < b.r) return true;
  if (a.r > b.r) return false;
  return a.nr < b.nr;
}
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    --a[i];
  }
  int q;
  cin >> q;
  vector<Pyt> pyt(q);
  for (int i = 0; i < q; ++i) {
    cin >> pyt[i].l >> pyt[i].r;
    --pyt[i].l;
    --pyt[i].r;
    pyt[i].nr = i;
  }
  sort(pyt.begin(), pyt.end(), por);
  vector<int> odp(q);
  int L = 0, R = 0;
  DodajPrawo(a[0], 0);
  for (int i = 0; i < q; ++i) {
    while (R > pyt[i].r) {
      UsunPrawo(a[R], R);
      --R;
    }
    while (R < pyt[i].r) {
      ++R;
      DodajPrawo(a[R], R);
    }
    while (L > pyt[i].l) {
      --L;
      DodajLewo(a[L], L);
    }
    while (L < pyt[i].l) {
      UsunLewo(a[L], L);
      ++L;
    }
    odp[pyt[i].nr] = rozne;
    if (zleCiagi == rozne) ++odp[pyt[i].nr];
  }
  for (int i = 0; i < q; ++i) cout << odp[i] << endl;
  return 0;
}
