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 inf = 1e12 + 47, MAXN = 1e5 + 47;
struct node {
  long long l, r, mn;
  node() {
    l = r = -1;
    mn = inf;
  }
};
vector<node> t(4 * MAXN);
void build(long long i, long long l, long long r) {
  t[i].l = l, t[i].r = r;
  if (l == r) return;
  long long mid = (l + r) / 2;
  build(i * 2, l, mid);
  build(i * 2 + 1, mid + 1, r);
  t[i].mn = min(t[i * 2].mn, t[i * 2 + 1].mn);
}
void change(long long i, long long pos, long long nv) {
  if (pos > t[i].r || t[i].l > pos) return;
  if (pos <= t[i].l && t[i].r <= pos) {
    t[i].mn = nv;
    return;
  }
  change(i * 2, pos, nv);
  change(i * 2 + 1, pos, nv);
  t[i].mn = min(t[i * 2].mn, t[i * 2 + 1].mn);
}
long long get_min(long long i, long long l, long long r) {
  if (l > t[i].r || t[i].l > r) return inf;
  if (l <= t[i].l && t[i].r <= r) return t[i].mn;
  return min(get_min(i * 2, l, r), get_min(i * 2 + 1, l, r));
}
long long n;
vector<long long> val, pr_c(MAXN, -1), cycl(MAXN, -1), pr(MAXN, -1),
    fc(MAXN, -1);
vector<vector<pair<long long, long long> > > quer;
vector<long long> fenw(MAXN, 0);
void update(long long ind, long long add) {
  for (long long i = ind; i < n; i = ((i + 1) | i)) fenw[i] += add;
}
long long get_sum(long long x) {
  long long res = 0;
  for (; x >= 0; x = (x & (x + 1)) - 1) res += fenw[x];
  return res;
}
long long get_sum(long long l, long long r) {
  if (l == 0) return get_sum(r);
  long long res = get_sum(r) - get_sum(l - 1);
  return res;
}
void print(long long i) {
  if (t[i].l == t[i].r)
    cout << t[i].mn << " ";
  else
    print(i * 2), print(i * 2 + 1);
}
void solve() {
  cin >> n;
  val.resize(n);
  for (long long i = 0; i < n; i++) cin >> val[i];
  long long q;
  cin >> q;
  quer.resize(n);
  for (long long i = 0; i < q; i++) {
    long long l, r;
    cin >> l >> r;
    --l, --r;
    quer[r].push_back({l, i});
  }
  build(1, 0, n - 1);
  vector<long long> ans(q, 1);
  for (long long i = 0; i < n; i++) {
    if (cycl[val[i]] != -1) {
      change(1, pr[val[i]], inf);
    }
    if (pr[val[i]] != -1 && i - pr[val[i]] != cycl[val[i]]) {
      cycl[val[i]] = i - pr[val[i]];
      pr_c[val[i]] = fc[val[i]];
    }
    if (pr[val[i]] == -1) {
      cycl[val[i]] = 0;
    } else {
      update(pr[val[i]], -1);
    }
    update(i, 1);
    change(1, i, pr_c[val[i]]);
    fc[val[i]] = pr[val[i]], pr[val[i]] = i;
    for (pair<long long, long long> p : quer[i]) {
      long long l = p.first, ind = p.second;
      long long mn = get_min(1, l, i);
      if (mn < l) ans[ind] = 0;
      ans[ind] += get_sum(l, i);
    }
  }
  for (long long i = 0; i < q; i++) cout << ans[i] << " ";
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long t = 1;
  while (t--) {
    solve();
  }
}
