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;
deque<int> pos[100010];
deque<int> diff[100010];
int cnt_occ[100010] = {0};
int cnt_period[100010] = {0};
int cnt_valid = 0;
int cnt_distinct = 0;
int a[100010];
int l[100010], r[100010];
pair<pair<int, int>, int> query[100010];
int ans[100010];
void add(int i, int val, bool f) {
  if (cnt_occ[val] >= 2) {
    int cur_diff = abs((f ? pos[val].back() : pos[val].front()) - i);
    int prev_diff = (f ? diff[val].back() : diff[val].front());
    if (cur_diff != prev_diff) {
      cnt_period[val]++;
      if (cnt_period[val] == 2) cnt_valid--;
    }
    if (!f)
      diff[val].push_front(cur_diff);
    else
      diff[val].push_back(cur_diff);
  } else if (cnt_occ[val] == 1) {
    int cur_diff = abs((f ? pos[val].back() : pos[val].front()) - i);
    diff[val].push_back(cur_diff);
    cnt_period[val]++;
  } else
    cnt_valid++;
  if (!f)
    pos[val].push_front(i);
  else
    pos[val].push_back(i);
  cnt_occ[val]++;
  if (cnt_occ[val] == 1) cnt_distinct++;
}
void rem(int i, int val, bool f) {
  if (cnt_occ[val] >= 3) {
    int cur_diff = (f ? diff[val].back() : diff[val].front());
    if (!f)
      diff[val].pop_front();
    else
      diff[val].pop_back();
    int prev_diff = (f ? diff[val].back() : diff[val].front());
    if (cur_diff != prev_diff) {
      cnt_period[val]--;
      if (cnt_period[val] == 1) cnt_valid++;
    }
  } else if (cnt_occ[val] == 2) {
    diff[val].pop_back();
    cnt_period[val]--;
  } else
    cnt_valid--;
  if (!f)
    pos[val].pop_front();
  else
    pos[val].pop_back();
  cnt_occ[val]--;
  if (cnt_occ[val] == 0) cnt_distinct--;
}
int main() {
  int n, q, i, j;
  scanf("%d", &n);
  for (i = 0; i < n; ++i) scanf("%d", &a[i]);
  int block = sqrt(n);
  scanf("%d", &q);
  for (i = 0; i < q; ++i) {
    scanf("%d", &l[i]);
    scanf("%d", &r[i]);
    --l[i];
    --r[i];
    query[i] = make_pair(make_pair(l[i] / block, r[i]), i);
  }
  sort(query, query + q);
  int p1 = 0, p2 = -1;
  for (j = 0; j < q; ++j) {
    i = query[j].second;
    while (p1 > l[i]) {
      --p1;
      add(p1, a[p1], 0);
    }
    while (p2 < r[i]) {
      ++p2;
      add(p2, a[p2], 1);
    }
    while (p1 < l[i]) {
      rem(p1, a[p1], 0);
      ++p1;
    }
    while (p2 > r[i]) {
      rem(p2, a[p2], 1);
      --p2;
    }
    ans[i] = cnt_distinct;
    if (!cnt_valid) ans[i]++;
  }
  for (i = 0; i < q; ++i) cout << ans[i] << '\n';
}
