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 col[100408];
int n;
int askl[100408], askr[100408];
int id[100408];
int ans[100408];
int cmp(int x, int y) {
  if (askl[x] / 400 != askl[y] / 400) return askl[x] / 400 < askl[y] / 400;
  return askr[x] < askr[y];
}
struct foronecolor {
  vector<int> con;
  int tot;
  void init(int x) {
    for (int i = 0; i <= x; i++) {
      con.push_back(0);
    }
    tot = 0;
  }
  void add(int x) {
    con[x]++;
    if (con[x] == 1) tot++;
  }
  void era(int x) {
    con[x]--;
    if (con[x] == 0) tot--;
  }
} trcyx[100408];
int lst[100408];
int pre[100408];
int nxt[100408];
int val[100408];
set<int> s[100408];
map<int, int> m[100408];
int now = 0;
int spj = 0;
int l = 1, r = 0;
int con[100408];
void ins(int x, int y, int t) {
  int cc = col[x];
  if (y == 1) {
    if (t == 1) {
      if (con[cc] and trcyx[cc].tot <= 1) spj--;
      if (nxt[x] <= r) trcyx[cc].add(val[nxt[x]]);
      if (trcyx[cc].tot <= 1) spj++;
    }
    if (t == 0) {
      if (con[cc] and trcyx[cc].tot <= 1) spj--;
      if (pre[x] >= l) trcyx[cc].add(val[x]);
      if (trcyx[cc].tot <= 1) spj++;
    }
  }
  if (y == -1) {
    if (t == 1) {
      if (trcyx[cc].tot <= 1) spj--;
      if (nxt[x] <= r) trcyx[cc].era(val[nxt[x]]);
      if (con[cc] > 1 and trcyx[cc].tot <= 1) spj++;
    }
    if (t == 0) {
      if (trcyx[cc].tot <= 1) spj--;
      if (pre[x] >= l) trcyx[cc].era(val[x]);
      if (con[cc] > 1 and trcyx[cc].tot <= 1) spj++;
    }
  }
  if (con[cc]) now--;
  con[cc] += y;
  if (con[cc]) now++;
}
int main() {
  int Q;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &col[i]);
  for (int i = 1; i <= n; i++) {
    pre[i] = lst[col[i]];
    nxt[lst[col[i]]] = i;
    lst[col[i]] = i;
    if (pre[i]) s[col[i]].insert(i - pre[i]);
  }
  for (int i = 1; i < 100408; i++) {
    int ct = 0;
    for (set<int>::iterator it = s[i].begin(); it != s[i].end(); it++)
      m[i][*it] = ct++;
    trcyx[i].init(ct);
  }
  for (int i = 1; i <= n; i++) val[i] = m[col[i]][i - pre[i]];
  scanf("%d", &Q);
  for (int i = 1; i <= Q; i++) scanf("%d%d", &askl[i], &askr[i]), id[i] = i;
  sort(id + 1, id + Q + 1, cmp);
  for (int iii = 1; iii <= Q; iii++) {
    int i = id[iii];
    while (l > askl[i]) l--, ins(l, 1, 1);
    while (r < askr[i]) r++, ins(r, 1, 0);
    while (l < askl[i]) ins(l, -1, 1), l++;
    while (r > askr[i]) ins(r, -1, 0), r--;
    ans[i] = now + 1 - (spj > 0);
  }
  for (int i = 1; i <= Q; i++) printf("%d\n", ans[i]);
  return 0;
}
