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;
template <typename T>
void get_int(T &x) {
  char t = getchar();
  bool neg = false;
  x = 0;
  for (; (t > '9' || t < '0') && t != '-'; t = getchar())
    ;
  if (t == '-') neg = true, t = getchar();
  for (; t <= '9' && t >= '0'; t = getchar()) x = x * 10 + t - '0';
  if (neg) x = -x;
}
template <typename T>
void print_int(T x) {
  if (x < 0) putchar('-'), x = -x;
  short a[20] = {}, sz = 0;
  while (x > 0) a[sz++] = x % 10, x /= 10;
  if (sz == 0) putchar('0');
  for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
const int inf = 0x3f3f3f3f;
const long long Linf = 1ll << 61;
const double pi = acos(-1.0);
const int magic = 300;
int n, q, a[100111], ans[100111], ord[100111], ql[100111], qr[100111];
int pre[100111], nxt[100111];
int curl, curr, tot, ok, cnt[100111], dif[100111];
bool cmp(int x, int y) {
  return make_pair(ql[x] / magic, qr[x]) < make_pair(ql[y] / magic, qr[y]);
}
void addr() {
  curr++;
  cnt[a[curr]]++;
  if (cnt[a[curr]] == 1) tot++;
  if (cnt[a[curr]] == 1)
    ok++;
  else if (cnt[a[curr]] > 2 && curr - pre[curr] != pre[curr] - pre[pre[curr]]) {
    if (dif[a[curr]] == 0) ok--;
    dif[a[curr]]++;
  }
}
void addl() {
  curl--;
  cnt[a[curl]]++;
  if (cnt[a[curl]] == 1) tot++;
  if (cnt[a[curl]] == 1)
    ok++;
  else if (cnt[a[curl]] > 2 && nxt[curl] - curl != nxt[nxt[curl]] - nxt[curl]) {
    if (dif[a[curl]] == 0) ok--;
    dif[a[curl]]++;
  }
}
void delr() {
  if (cnt[a[curr]] == 1) tot--;
  if (cnt[a[curr]] == 1)
    ok--;
  else if (cnt[a[curr]] > 2 && curr - pre[curr] != pre[curr] - pre[pre[curr]]) {
    dif[a[curr]]--;
    if (dif[a[curr]] == 0) ok++;
  }
  cnt[a[curr]]--;
  curr--;
}
void dell() {
  if (cnt[a[curl]] == 1) tot--;
  if (cnt[a[curl]] == 1)
    ok--;
  else if (cnt[a[curl]] > 2 && nxt[curl] - curl != nxt[nxt[curl]] - nxt[curl]) {
    dif[a[curl]]--;
    if (dif[a[curl]] == 0) ok++;
  }
  cnt[a[curl]]--;
  curl++;
}
int main() {
  get_int(n);
  for (int i = 1; i <= n; i++) {
    get_int(a[i]);
    if (cnt[a[i]]) {
      pre[i] = cnt[a[i]];
      nxt[cnt[a[i]]] = i;
    }
    cnt[a[i]] = i;
  }
  memset(cnt, 0, sizeof(cnt));
  get_int(q);
  for (int i = 1; i <= q; i++) {
    ord[i] = i;
    get_int(ql[i]), get_int(qr[i]);
  }
  sort(ord + 1, ord + q + 1, cmp);
  for (int _ = 1; _ <= q; _++) {
    int i = ord[_];
    while (curr < qr[i]) addr();
    while (curl > ql[i]) addl();
    while (curr > qr[i]) delr();
    while (curl < ql[i]) dell();
    ans[i] = tot + (ok == 0);
  }
  for (int i = 1; i <= q; i++) print_int(ans[i]), puts("");
  return 0;
}
