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[100005];
map<int, int> nu;
int las[100005], pos[100005] = {0};
int tot = 0;
int b1[100005], b2[100005];
int q;
struct query {
  int l, r;
  int i;
} qu[100005];
int cmp(const query &a, const query &b) { return a.r < b.r; }
void bitupd(int *a, int i, int del) {
  for (; i <= n; i += i & -i) a[i] += del;
}
int bitque(int *a, int i) {
  int ans = 0;
  for (; i; i -= i & -i) ans += a[i];
  return ans;
}
int cnt[100005] = {0}, d[100005];
int start[100005] = {0};
int ans[100005];
void add(int i) {
  bitupd(b1, las[i] + 1, 1);
  bitupd(b1, i + 1, -1);
  if (cnt[a[i]] == 0) {
    bitupd(b2, 1, 1);
    bitupd(b2, i + 1, -1);
    start[a[i]] = 1;
  } else if (cnt[a[i]] == 1) {
    bitupd(b2, las[i] + 1, 1);
    bitupd(b2, i + 1, -1);
    d[a[i]] = i - las[i];
  } else {
    if (d[a[i]] == i - las[i]) {
      bitupd(b2, las[i] + 1, 1);
      bitupd(b2, i + 1, -1);
    } else {
      bitupd(b2, start[a[i]], -1);
      bitupd(b2, las[las[i]] + 1, 1);
      start[a[i]] = las[las[i]] + 1;
      bitupd(b2, las[i] + 1, 1);
      bitupd(b2, i + 1, -1);
      d[a[i]] = i - las[i];
    }
  }
  cnt[a[i]]++;
}
int ask(int l) {
  int x = bitque(b1, l) + 1;
  if (bitque(b2, l)) x--;
  return x;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), nu[a[i]];
  for (map<int, int>::iterator it = nu.begin(); it != nu.end(); it++)
    it->second = ++tot;
  for (int i = 1; i <= n; i++) a[i] = nu[a[i]];
  for (int i = 1; i <= n; i++) {
    las[i] = pos[a[i]];
    pos[a[i]] = i;
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) scanf("%d%d", &qu[i].l, &qu[i].r), qu[i].i = i;
  sort(qu + 1, qu + 1 + q, cmp);
  int cur = 1;
  for (int r = 1; r <= n; r++) {
    add(r);
    while (cur <= q && qu[cur].r == r) ans[qu[cur].i] = ask(qu[cur].l), cur++;
  }
  for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
  return 0;
}
