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 N = 100005;
struct Data {
  int l, r, id;
} Q[N];
int sq;
bool cmp(Data A, Data B) {
  if (A.l / sq != B.l / sq) return A.l / sq < B.l / sq;
  return A.r < B.r;
}
int n, ara[N], q, answer[N];
int Uni, freq[N], Good, Goodness[N];
deque<int> D[N];
unordered_multiset<int> st[N];
unordered_set<int> un[N];
void add(int in, int dir) {
  int v = ara[in];
  freq[v]++;
  if (freq[v] == 1) {
    Uni++;
    if (Goodness[v] == 0) Good++;
    Goodness[v] = 1;
  }
  if (dir == 0) {
    D[v].push_front(in);
    int sz = D[v].size();
    if (sz >= 2) {
      st[v].insert(((D[v][0] - D[v][1]) < 0 ? -(D[v][0] - D[v][1])
                                            : (D[v][0] - D[v][1])));
      un[v].insert(((D[v][0] - D[v][1]) < 0 ? -(D[v][0] - D[v][1])
                                            : (D[v][0] - D[v][1])));
      if (un[v].size() > 1 and Goodness[v] == 1) {
        Good--;
        Goodness[v] = 0;
      }
    }
  } else {
    D[v].push_back(in);
    int sz = D[v].size();
    if (sz >= 2) {
      st[v].insert(((D[v][sz - 1] - D[v][sz - 2]) < 0
                        ? -(D[v][sz - 1] - D[v][sz - 2])
                        : (D[v][sz - 1] - D[v][sz - 2])));
      un[v].insert(((D[v][sz - 1] - D[v][sz - 2]) < 0
                        ? -(D[v][sz - 1] - D[v][sz - 2])
                        : (D[v][sz - 1] - D[v][sz - 2])));
      if (un[v].size() > 1 and Goodness[v] == 1) {
        Good--;
        Goodness[v] = 0;
      }
    }
  }
}
void remove(int in, int dir) {
  int v = ara[in];
  freq[v]--;
  if (freq[v] == 0) {
    Uni--;
    if (Goodness[v] == 1) Good--;
    Goodness[v] = 0;
  }
  if (dir == 0) {
    if (D[v].size() >= 2) {
      int value = ((D[v][0] - D[v][1]) < 0 ? -(D[v][0] - D[v][1])
                                           : (D[v][0] - D[v][1]));
      st[v].erase(st[v].find(value));
      if (st[v].find(value) == st[v].end()) un[v].erase(value);
      if (un[v].size() <= 1 and Goodness[v] == 0) {
        Good++;
        Goodness[v] = 1;
      }
    }
    D[v].pop_front();
  } else {
    if (D[v].size() >= 2) {
      int sz = D[v].size();
      int value =
          ((D[v][sz - 1] - D[v][sz - 2]) < 0 ? -(D[v][sz - 1] - D[v][sz - 2])
                                             : (D[v][sz - 1] - D[v][sz - 2]));
      st[v].erase(st[v].find(value));
      if (st[v].find(value) == st[v].end()) un[v].erase(value);
      if (un[v].size() <= 1 and Goodness[v] == 0) {
        Good++;
        Goodness[v] = 1;
      }
    }
    D[v].pop_back();
  }
}
int main() {
  scanf("%d", &n);
  sq = sqrt(n) + 1;
  for (int i = 1; i <= n; i++) scanf("%d", &ara[i]);
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int l, r;
    scanf("%d %d", &l, &r);
    Q[i].l = l;
    Q[i].r = r;
    Q[i].id = i;
  }
  sort(Q, Q + q, cmp);
  int mol = 1;
  int mor = 0;
  for (int i = 0; i < q; i++) {
    int l = Q[i].l;
    int r = Q[i].r;
    while (mor < r) {
      mor++;
      add(mor, 1);
    }
    while (mor > r) {
      remove(mor, 1);
      mor--;
    }
    while (mol < l) {
      remove(mol, 0);
      mol++;
    }
    while (mol > l) {
      mol--;
      add(mol, 0);
    }
    assert(Good >= 0 and Uni >= 0);
    answer[Q[i].id] = Uni + (Good == 0);
  }
  for (int i = 0; i < q; i++) printf("%d\n", answer[i]);
}
