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 sq = 300;
int n, arr[100005], q, a, b, forwardd[100005], backward[100005];
int ans[100005], l, r, num, ok, occ[100005], numjumps[100005], arith[100005];
unordered_map<int, int> jumps[100005];
vector<pair<pair<int, int>, int> > v;
bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
  return pair<int, int>(a.first.first / sq, a.first.second) <
         pair<int, int>(b.first.first / sq, b.first.second);
}
void addbeg(int val, int idx) {
  ok -= arith[val];
  occ[val]++;
  if (occ[val] == 1) {
    num++;
    arith[val] = 1;
  } else {
    jumps[val][forwardd[idx] - idx]++;
    if (jumps[val][forwardd[idx] - idx] == 1) numjumps[val]++;
    if (numjumps[val] == 1)
      arith[val] = 1;
    else
      arith[val] = 0;
  }
  ok += arith[val];
}
void addend(int val, int idx) {
  ok -= arith[val];
  occ[val]++;
  if (occ[val] == 1) {
    num++;
    arith[val] = 1;
  } else {
    jumps[val][idx - backward[idx]]++;
    if (jumps[val][idx - backward[idx]] == 1) numjumps[val]++;
    if (numjumps[val] == 1)
      arith[val] = 1;
    else
      arith[val] = 0;
  }
  ok += arith[val];
}
void rembeg(int val, int idx) {
  ok -= arith[val];
  occ[val]--;
  if (occ[val] == 0) {
    num--;
    arith[val] = 0;
  } else {
    jumps[val][forwardd[idx] - idx]--;
    if (jumps[val][forwardd[idx] - idx] == 0) numjumps[val]--;
    if (numjumps[val] == 1 or occ[val] == 1)
      arith[val] = 1;
    else
      arith[val] = 0;
  }
  ok += arith[val];
}
void remend(int val, int idx) {
  ok -= arith[val];
  occ[val]--;
  if (occ[val] == 0) {
    num--;
    arith[val] = 0;
  } else {
    jumps[val][idx - backward[idx]]--;
    if (jumps[val][idx - backward[idx]] == 0) numjumps[val]--;
    if (numjumps[val] == 1 or occ[val] == 1)
      arith[val] = 1;
    else
      arith[val] = 0;
  }
  ok += arith[val];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  map<int, int> pre;
  for (int i = (int)0; i <= (int)n - 1; i++) {
    cin >> arr[i];
    if (pre.find(arr[i]) != pre.end()) {
      forwardd[pre[arr[i]]] = i;
      backward[i] = pre[arr[i]];
    }
    pre[arr[i]] = i;
  }
  cin >> q;
  for (int i = (int)1; i <= (int)q; i++) {
    cin >> a >> b;
    a--;
    b--;
    v.push_back(pair<pair<int, int>, int>(pair<int, int>(a, b), i));
  }
  sort(v.begin(), v.end(), cmp);
  for (auto it : v) {
    a = it.first.first, b = it.first.second;
    while (r <= b) {
      addend(arr[r], r);
      r++;
    }
    while (r - 1 > b) {
      r--;
      remend(arr[r], r);
    }
    while (l < a) {
      rembeg(arr[l], l);
      l++;
    }
    while (l > a) {
      l--;
      addbeg(arr[l], l);
    }
    ans[it.second] += num + (ok == 0);
  }
  for (int i = (int)1; i <= (int)q; i++) cout << ans[i] << "\n";
}
