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, SQRT = 550, Q = 100005;
struct Query {
  int l, r, id;
  Query() {}
  Query(int l, int r, int id) : l(l), r(r), id(id) {}
  bool operator<(const Query& q) const {
    if (l / SQRT != q.l / SQRT) return l / SQRT < q.l / SQRT;
    return r > q.r;
  }
} queries[Q];
int arr[N], n, k;
int freq[2000005], answer[Q], mo_distinct, mo_has_special;
deque<int> nums[N];
map<int, int> freq_ratio[N];
inline void add_elem(int x) {
  if (++freq[x] == 1) ++mo_distinct;
}
inline void rem_elem(int x) {
  if (freq[x]-- == 1) --mo_distinct;
}
inline void add_freq_ratio(int x, int diff) {
  bool before = freq_ratio[x].size() <= 1;
  ++freq_ratio[x][diff];
  bool after = freq_ratio[x].size() == 1;
  if (!before && after) ++mo_has_special;
  if (before && !after) --mo_has_special;
}
inline void rem_freq_ratio(int x, int diff) {
  bool before = freq_ratio[x].size() == 1;
  --freq_ratio[x][diff];
  if (freq_ratio[x][diff] == 0) freq_ratio[x].erase(diff);
  bool after = freq_ratio[x].size() <= 1;
  if (!before && after) ++mo_has_special;
  if (before && !after) --mo_has_special;
}
void add_begin(int idx, int x) {
  add_elem(x);
  if (!nums[x].empty()) {
    int diff = nums[x].front() - idx;
    add_freq_ratio(x, diff);
  } else {
    ++mo_has_special;
  }
  nums[x].push_front(idx);
}
void add_end(int idx, int x) {
  add_elem(x);
  if (!nums[x].empty()) {
    int diff = idx - nums[x].back();
    add_freq_ratio(x, diff);
  } else {
    ++mo_has_special;
  }
  nums[x].push_back(idx);
}
void rem_begin(int x) {
  rem_elem(x);
  int front = nums[x].front();
  nums[x].pop_front();
  if (!nums[x].empty()) {
    int diff = nums[x].front() - front;
    rem_freq_ratio(x, diff);
  } else {
    --mo_has_special;
  }
}
void rem_end(int x) {
  rem_elem(x);
  int back = nums[x].back();
  nums[x].pop_back();
  if (!nums[x].empty()) {
    int diff = back - nums[x].back();
    rem_freq_ratio(x, diff);
  } else {
    --mo_has_special;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", &arr[i]);
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    int l, r;
    scanf("%d %d", &l, &r);
    --l;
    --r;
    queries[i] = Query(l, r, i);
  }
  sort(queries, queries + q);
  mo_has_special = mo_distinct = 0;
  int l = 0, r = 0;
  add_end(0, arr[0]);
  for (int i = 0; i < q; ++i) {
    while (r < queries[i].r) {
      ++r;
      add_end(r, arr[r]);
    }
    while (l > queries[i].l) {
      --l;
      add_begin(l, arr[l]);
    }
    while (l < queries[i].l) rem_begin(arr[l++]);
    while (r > queries[i].r) rem_end(arr[r--]);
    if (mo_has_special != 0) {
      answer[queries[i].id] = mo_distinct;
    } else {
      answer[queries[i].id] = mo_distinct + 1;
    }
  }
  for (int i = 0; i < q; ++i) printf("%d\n", answer[i]);
  return 0;
}
