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>
#pragma GCC optimize("O2")
using namespace std;
const int maxn = 1000 * 100 + 5;
const long long inf = 9223372036854775807;
const long long mod = 1e9 + 7;
vector<int> ls[maxn], v[maxn];
int a[maxn], seg[4 * maxn], fp[maxn], fen[maxn], n, q, l[maxn], r[maxn],
    d[maxn], is[maxn], sz = 1;
void upd(int i, int v) {
  if (i == 0) return;
  for (; i < maxn; i += i & -i) fen[i] += v;
}
int get(int i) {
  int s = 0;
  for (; i > 0; i -= i & -i) s += fen[i];
  return s;
}
void supd(int i, int v, int x = 1, int lx = 0, int rx = sz) {
  if (rx - lx == 1) {
    seg[x] = v;
    return;
  }
  int mid = lx + rx >> 1;
  if (i < mid)
    supd(i, v, x << 1, lx, mid);
  else
    supd(i, v, x << 1 | 1, mid, rx);
  seg[x] = min(seg[x << 1], seg[x << 1 | 1]);
}
int sget(int l, int r, int x = 1, int lx = 0, int rx = sz) {
  if (lx >= r || rx <= l) return mod;
  if (lx >= l && rx <= r) {
    return seg[x];
  }
  int mid = lx + rx >> 1;
  return min(sget(l, r, x << 1, lx, mid), sget(l, r, x << 1 | 1, mid, rx));
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  fill(seg, seg + 4 * maxn, mod);
  cin >> n;
  while (sz <= n) sz *= 2;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (ls[a[i]].size() == 0) ls[a[i]].push_back(0);
  }
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> l[i] >> r[i];
    v[r[i]].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    int sz = (int)ls[a[i]].size();
    upd(ls[a[i]][sz - 1], -1);
    ls[a[i]].push_back(i);
    sz++;
    upd(ls[a[i]][sz - 1], 1);
    supd(ls[a[i]][sz - 2], mod);
    if (sz <= 3) {
      supd(i, 0);
    } else {
      if (ls[a[i]][sz - 1] - ls[a[i]][sz - 2] !=
          ls[a[i]][sz - 2] - ls[a[i]][sz - 3]) {
        fp[a[i]] = ls[a[i]][sz - 3];
      }
      supd(i, fp[a[i]]);
    }
    for (auto u : v[i]) {
      d[u] = get(i) - get(l[u] - 1);
      if (l[u] > sget(l[u], i + 1))
        is[u] = 0;
      else
        is[u] = 1;
    }
  }
  for (int i = 1; i <= q; i++) {
    cout << d[i] + is[i] << endl;
  }
}
