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 int Maxn = 100005;
const int Maxp = 320;
const int Maxm = 524288;
int n;
int b[Maxn];
int nxtbyval[Maxn];
int nxt[Maxn];
int prvbyval[Maxn];
deque<int> seq[Maxn];
vector<pair<int, int> > st[Maxm];
int dif;
int cnt[Maxn];
int q;
int qa[Maxn], qb[Maxn];
vector<pair<int, int> > bylef[Maxp];
int res[Maxn];
void Update(int v, int l, int r, int a, int b, int lef, int rig) {
  if (l == a && r == b) {
    while (!st[v].empty() && lef <= st[v].back().first) st[v].pop_back();
    if (!st[v].empty() && lef <= st[v].back().second + 1)
      st[v].back().second = rig;
    else
      st[v].push_back(pair<int, int>(lef, rig));
  } else {
    int m = l + r >> 1;
    if (a <= m) Update(2 * v, l, m, a, min(m, b), lef, rig);
    if (m + 1 <= b) Update(2 * v + 1, m + 1, r, max(m + 1, a), b, lef, rig);
  }
}
bool Get(int v, int l, int r, int x, int y) {
  int ind = lower_bound(st[v].begin(), st[v].end(), pair<int, int>(y + 1, 0)) -
            st[v].begin() - 1;
  if (ind >= 0 && y <= st[v][ind].second) return true;
  if (l == r)
    return false;
  else {
    int m = l + r >> 1;
    if (x <= m)
      return Get(2 * v, l, m, x, y);
    else
      return Get(2 * v + 1, m + 1, r, x, y);
  }
}
void Add(int ind) {
  if (cnt[b[ind]]++ == 0) dif++;
}
void Rem(int ind) {
  if (--cnt[b[ind]] == 0) dif--;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  fill(nxtbyval, nxtbyval + Maxn, n + 1);
  for (int i = n; i > 0; i--) {
    nxt[i] = nxtbyval[b[i]];
    nxtbyval[b[i]] = i;
  }
  for (int i = 1; i <= n; i++) {
    seq[b[i]].push_back(i);
    while (seq[b[i]].size() >= 2 &&
           seq[b[i]][1] - seq[b[i]].front() !=
               seq[b[i]].back() - seq[b[i]][int(seq[b[i]].size()) - 2]) {
      prvbyval[b[i]] = seq[b[i]].front();
      seq[b[i]].pop_front();
    }
    Update(1, 1, n, i, nxt[i] - 1, prvbyval[b[i]] + 1, i);
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d %d", &qa[i], &qb[i]);
    if (qb[i] - qa[i] <= Maxp) {
      for (int j = qa[i]; j <= qb[i]; j++) Add(j);
      res[i] = dif;
      for (int j = qa[i]; j <= qb[i]; j++) Rem(j);
    } else
      bylef[qa[i] / Maxp + 1].push_back(pair<int, int>(qb[i], i));
  }
  for (int i = 0; i < Maxp; i++) {
    sort(bylef[i].begin(), bylef[i].end());
    int L = i * Maxp, R = i * Maxp;
    for (int j = 0; j < bylef[i].size(); j++) {
      int ind = bylef[i][j].second;
      while (R <= qb[ind]) Add(R++);
      while (qa[ind] < L) Add(--L);
      while (L < qa[ind]) Rem(L++);
      res[ind] = dif;
    }
    while (L < R) Rem(L++);
  }
  for (int i = 1; i <= q; i++)
    printf("%d\n", res[i] + 1 - int(Get(1, 1, n, qb[i], qa[i])));
  return 0;
}
