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;
int n;
int a[1000005], fr[1000005], bad[1000005], lft[1000005], dist[1000005],
    ok[1000005], aib[1000005];
int aint[3 * 1000005];
struct el {
  int l, ind;
};
vector<el> L[1000005];
inline int qry(int p) {
  int sol = 0;
  for (int i = p; i <= n; i += (i & (-i))) sol += aib[i];
  return sol;
}
inline void upd(int p, int val) {
  for (int i = p; i > 0; i -= (i & (-i))) aib[i] += val;
}
inline void Build(int nod, int st, int dr) {
  if (st == dr) {
    aint[nod] = 2000000000;
    return;
  }
  int mij = ((st + dr) >> 1);
  Build(2 * nod, st, mij);
  Build(2 * nod + 1, mij + 1, dr);
  aint[nod] = max(aint[2 * nod], aint[2 * nod + 1]);
}
inline void Update(int nod, int st, int dr, int x, int y) {
  if (st == dr) {
    aint[nod] = y;
    return;
  }
  int mij = ((st + dr) >> 1);
  if (x <= mij)
    Update(2 * nod, st, mij, x, y);
  else
    Update(2 * nod + 1, mij + 1, dr, x, y);
  aint[nod] = min(aint[2 * nod], aint[2 * nod + 1]);
}
inline int Query(int nod, int st, int dr, int x, int y) {
  if (st == x && y == dr) return aint[nod];
  int mij = ((st + dr) >> 1);
  if (y <= mij)
    return Query(2 * nod, st, mij, x, y);
  else if (x > mij)
    return Query(2 * nod + 1, mij + 1, dr, x, y);
  else
    return min(Query(2 * nod, st, mij, x, mij),
               Query(2 * nod + 1, mij + 1, dr, mij + 1, y));
}
inline void Solve() {
  int i;
  for (i = 1; i <= n; ++i) {
    upd(i, 1);
    if (lft[i]) upd(lft[i], -1);
    Update(1, 1, n, i, bad[i]);
    if (lft[i]) Update(1, 1, n, lft[i], 2000000000);
    for (vector<el>::iterator itt = L[i].begin(); itt != L[i].end(); ++itt) {
      el it = *itt;
      dist[it.ind] = qry(it.l);
      ok[it.ind] = (Query(1, 1, n, it.l, i) < it.l);
    }
  }
}
int main() {
  int i, j, l, r, q, x;
  el w;
  cin.sync_with_stdio(0);
  cin >> n;
  for (i = 1; i <= n; ++i) cin >> a[i];
  for (i = 1; i <= n; ++i) {
    lft[i] = fr[a[i]];
    if (lft[i] && lft[lft[i]])
      if (lft[lft[i]] == i - 2 * (i - lft[i]))
        bad[i] = bad[lft[i]];
      else
        bad[i] = lft[lft[i]];
    fr[a[i]] = i;
  }
  cin >> q;
  for (i = 1; i <= q; ++i) {
    cin >> w.l >> x;
    w.ind = i;
    L[x].push_back(w);
  }
  Solve();
  for (i = 1; i <= q; ++i) cout << dist[i] + 1 - ok[i] << "\n";
  return 0;
}
