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 long long MAXN = 1e5 + 10;
const long long INF = 1e12 + 10;
vector<pair<long long, long long> > query[MAXN];
long long a[MAXN];
long long ans[MAXN];
long long pre[MAXN];
long long fen[MAXN];
long long last[MAXN];
long long segt[4 * MAXN];
long long period[MAXN];
void fen_update(long long id, long long val) {
  for (; id < MAXN; id += ((id) & -(id))) {
    fen[id] += val;
  }
}
long long fen_get_sum(long long id) {
  long long sum = 0;
  for (; id > 0; id -= ((id) & -(id))) {
    sum += fen[id];
  }
  return sum;
}
void set_update(long long id, long long L, long long R, long long p,
                long long val) {
  if (L > p || R < p) {
    return;
  }
  if (L == R) {
    segt[id] = val;
    return;
  }
  long long mid = (L + R) / 2;
  set_update(2 * id + 1, L, mid, p, val);
  set_update(2 * id + 2, mid + 1, R, p, val);
  segt[id] = min(segt[2 * id + 1], segt[2 * id + 2]);
}
long long get_min(long long id, long long L, long long R, long long l,
                  long long r) {
  if (L > r || R < l) {
    return INF;
  }
  if (L >= l && R <= r) {
    return segt[id];
  }
  long long mid = (L + R) / 2;
  return min(get_min(2 * id + 1, L, mid, l, r),
             get_min(2 * id + 2, mid + 1, R, l, r));
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    pre[i] = last[a[i]];
    period[i] =
        (i - pre[i] == pre[i] - pre[pre[i]] ? period[pre[i]] : pre[pre[i]]);
    last[a[i]] = i;
  }
  long long q;
  cin >> q;
  for (long long i = 0; i < q; i++) {
    long long l, r;
    cin >> l >> r;
    query[r].push_back(make_pair(l, i));
  }
  for (long long i = 1; i <= n; i++) {
    if (pre[i] != 0) {
      fen_update(pre[i], -1);
      set_update(0, 1, n, pre[i], INF);
    }
    fen_update(i, 1);
    set_update(0, 1, n, i, period[i]);
    for (long long j = 0; j < query[i].size(); j++) {
      ans[query[i][j].second] =
          fen_get_sum(i) - fen_get_sum(query[i][j].first - 1) -
          (get_min(0, 1, n, query[i][j].first, i) < query[i][j].first) + 1;
    }
  }
  for (long long i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}
