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;
template <class T1, class T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return is >> p.first >> p.second;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
  return os << p.first << " " << p.second;
}
const int N = 100005;
const int magic = 300;
struct zapr {
  int l, r, num;
  zapr(int l = 0, int r = 0, int num = 0) : l(l), r(r), num(num) {}
  bool operator<(const zapr &t) const {
    if (l / magic == t.l / magic) return r < t.r;
    return l < t.l;
  }
};
int ans[N];
pair<int, int> dp[N];
int lst[N];
list<int> positions[N];
int cou[N];
int razl = 0;
int n_good = 0;
int a[N];
void insert(int x) {
  if (cou[x]++ == 0) razl++;
}
void remove(int x) {
  if (cou[x]-- == 1) razl--;
}
int is_good(int x) {
  return positions[a[x]].empty()
             ? 0
             : dp[positions[a[x]].front()].first >= positions[a[x]].back();
}
int cur_l, cur_r;
void inc_r() {
  cur_r++;
  n_good -= is_good(cur_r);
  positions[a[cur_r]].push_back(cur_r);
  n_good += is_good(cur_r);
  insert(a[cur_r]);
}
void dec_r() {
  n_good -= is_good(cur_r);
  positions[a[cur_r]].pop_back();
  n_good += is_good(cur_r);
  remove(a[cur_r]);
  cur_r--;
}
void inc_l() {
  n_good -= is_good(cur_l);
  positions[a[cur_l]].pop_front();
  n_good += is_good(cur_l);
  remove(a[cur_l]);
  cur_l++;
}
void dec_l() {
  cur_l--;
  n_good -= is_good(cur_l);
  positions[a[cur_l]].push_front(cur_l);
  n_good += is_good(cur_l);
  insert(a[cur_l]);
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = n - 1; i >= 0; i--) {
    if (lst[a[i]]) {
      if (lst[a[i]] - i == dp[lst[a[i]]].second)
        dp[i] = dp[lst[a[i]]];
      else
        dp[i] = {lst[a[i]], lst[a[i]] - i};
    } else
      dp[i].first = i;
    lst[a[i]] = i;
  }
  int q;
  cin >> q;
  vector<zapr> z(q);
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    z[i] = {l - 1, r - 1, i};
  }
  sort(z.begin(), z.end());
  cur_l = 0, cur_r = -1;
  for (int zz = 0; zz < q; zz++) {
    int l = z[zz].l, r = z[zz].r;
    while (cur_r < r) inc_r();
    while (cur_l > l) dec_l();
    while (cur_r > r) dec_r();
    while (cur_l < l) inc_l();
    ans[z[zz].num] = razl;
    if (!n_good) ans[z[zz].num]++;
  }
  for (int i = 0; i < q; i++) cout << ans[i] << "\n";
}
