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 xx[4] = {0, 0, 1, -1};
int yy[4] = {1, -1, 0, 0};
int n, m;
deque<int> d[int(1e5 + 100)];
int a[int(1e5 + 100)], f[int(1e5 + 100)], cnt[int(1e5 + 100)],
    lef[int(1e5 + 100)], t[int(1e5 + 100)], pre[int(1e5 + 100)];
int ans[int(1e5 + 100)];
struct re {
  int l, r, i;
};
int len;
re q[int(1e5 + 100)];
bool cmp(re a, re b) {
  if (a.l / len < b.l / len || (a.l / len == b.l / len && a.r < b.r))
    return true;
  return false;
}
int main() {
  scanf("%d", &n);
  for (int i = (1), _b = (n); i <= _b; i++) scanf("%d", &a[i]);
  for (int i = (1), _b = (n); i <= _b; i++) {
    int x = a[i];
    pre[i] = t[x];
    if (pre[i] == 0) {
      lef[i] = 1;
    } else {
      int x2 = pre[i];
      int x1 = pre[x2];
      if (x1 == 0) {
        lef[i] = 2;
      } else {
        if (i - x2 == x2 - x1) {
          lef[i] = lef[x2] + 1;
        } else
          lef[i] = 2;
      }
    }
    t[x] = i;
  }
  scanf("%d", &m);
  len = int(sqrt(n)) + 1;
  for (int i = (1), _b = (m); i <= _b; i++) {
    scanf("%d%d", &q[i].l, &q[i].r);
    q[i].i = i;
  }
  sort(q + 1, q + 1 + m, cmp);
  int l = 1, r = 1, w = 1, s = 1;
  f[a[1]]++;
  d[a[1]].push_back(1);
  cnt[a[1]] = true;
  int save;
  for (int i = (1), _b = (m); i <= _b; i++) {
    int x, tmp;
    while (r < q[i].r) {
      r++;
      x = a[r];
      f[x]++;
      if (f[x] == 1) s++;
      w -= cnt[x];
      d[x].push_back(r);
      if (!d[x].empty() && lef[d[x].back()] >= d[x].size())
        cnt[x] = true;
      else
        cnt[x] = false;
      w += cnt[x];
    }
    while (l > q[i].l) {
      l--;
      x = a[l];
      f[x]++;
      if (f[x] == 1) s++;
      w -= cnt[x];
      d[x].push_front(l);
      if (!d[x].empty() && lef[d[x].back()] >= d[x].size())
        cnt[x] = true;
      else
        cnt[x] = false;
      w += cnt[x];
    }
    while (r > q[i].r) {
      x = a[r];
      f[x]--;
      if (f[x] == 0) s--;
      w -= cnt[x];
      d[x].pop_back();
      if (!d[x].empty() && lef[d[x].back()] >= d[x].size())
        cnt[x] = true;
      else
        cnt[x] = false;
      w += cnt[x];
      r--;
    }
    while (l < q[i].l) {
      x = a[l];
      f[x]--;
      if (f[x] == 0) s--;
      w -= cnt[x];
      d[x].pop_front();
      if (!d[x].empty() && lef[d[x].back()] >= d[x].size())
        cnt[x] = true;
      else
        cnt[x] = false;
      w += cnt[x];
      l++;
    }
    if (w)
      ans[q[i].i] = s;
    else
      ans[q[i].i] = s + 1;
  }
  for (int i = (1), _b = (m); i <= _b; i++) {
    printf("%d\n", ans[i]);
  }
}
