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 oo = 0x3f3f3f3f;
const double eps = 1e-9;
int nums[100100], N;
int BIT[200100];
int lastPos[100100];
int nextId[100100];
int prevId[100100];
int maxTo[100100];
int segTree[400400];
void updateBIT(int n, int v) {
  while (n < 200100) {
    BIT[n] += v;
    n += n & (-n);
  }
}
int queryBIT(int n) {
  int res = 0;
  for (; n; n -= n & (-n)) res += BIT[n];
  return res;
}
int glchild(int n) { return 2 * n + 1; }
int gmid(int from, int to) { return from + (to - from) / 2; }
int getMaxId(int i1, int i2) {
  if (i1 == -1) return i2;
  if (i2 == -1) return i1;
  return ((maxTo[i1] > maxTo[i2]) ? i1 : i2);
}
int maxChild(int n) {
  int c1 = glchild(n);
  int c2 = c1 + 1;
  return getMaxId(segTree[c1], segTree[c2]);
}
void init(int n, int from, int to) {
  if (from + 1 == to) {
    segTree[n] = from;
  } else {
    int c1 = glchild(n);
    int c2 = c1 + 1;
    int mid = gmid(from, to);
    init(c1, from, mid);
    init(c2, mid, to);
    segTree[n] = maxChild(n);
  }
}
void init() { init(0, 0, N); }
void update(int n, int from, int to, int pos, int val) {
  if (pos >= to || pos < from) return;
  if (from + 1 == to) {
    maxTo[pos] = val;
    return;
  }
  int c1 = glchild(n);
  int c2 = c1 + 1;
  int mid = gmid(from, to);
  update(c1, from, mid, pos, val);
  update(c2, mid, to, pos, val);
  segTree[n] = maxChild(n);
}
void update(int pos, int val) { update(0, 0, N, pos, val); }
int query(int n, int from, int to, int ifrom, int ito) {
  if (from >= ifrom && to <= ito) return segTree[n];
  if (from >= ito) return -1;
  if (to <= ifrom) return -1;
  int c1 = glchild(n);
  int c2 = c1 + 1;
  int mid = gmid(from, to);
  int res = getMaxId(query(c1, from, mid, ifrom, ito),
                     query(c2, mid, to, ifrom, ito));
  return res;
}
int query(int ifrom, int ito) { return query(0, 0, N, ifrom, ito); }
pair<pair<int, int>, int> queries[100100];
int queryres[100100];
int main() {
  memset(BIT, 0, sizeof(BIT));
  cin >> N;
  for (int i = (0); i < (N); i++) cin >> nums[i];
  memset(lastPos, -1, sizeof(lastPos));
  for (int i = (0); i < (N); i++) {
    prevId[i] = lastPos[nums[i]];
    if (prevId[i] != -1) {
      nextId[prevId[i]] = i;
    }
    lastPos[nums[i]] = i;
    nextId[i] = N + 2;
  }
  for (int i = int(N) - 1; i >= (0); i--) {
    if (nextId[i] == N + 2 || nextId[nextId[i]] == N + 2)
      maxTo[i] = N + 2;
    else {
      int j = nextId[i];
      int k = nextId[j];
      if (k - j == j - i)
        maxTo[i] = maxTo[j];
      else
        maxTo[i] = k;
    }
  }
  init();
  int Q;
  cin >> Q;
  for (int i = (0); i < (Q); i++) {
    cin >> queries[i].first.first >> queries[i].first.second;
    --queries[i].first.first;
    queries[i].second = i;
  }
  sort(queries, queries + Q);
  int CID = N;
  for (int q = int(Q) - 1; q >= (0); q--) {
    int f = queries[q].first.first;
    int t = queries[q].first.second;
    int bn = -1;
    while (bn == -1) {
      bn = query(f, t);
      if (prevId[bn] >= f) {
        update(bn, -1);
        bn = -1;
      }
    }
    int res = 0;
    if (maxTo[bn] < t) ++res;
    while (CID > f) {
      --CID;
      updateBIT(CID + 1, 1);
      if (nextId[CID] < N) {
        updateBIT(nextId[CID] + 1, -1);
      }
    }
    res += queryBIT(t);
    queryres[queries[q].second] = res;
  }
  for (int i = (0); i < (Q); i++) cout << queryres[i] << endl;
  return 0;
}
