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 double eps = 1E-8;
const int dx4[4] = {1, 0, 0, -1};
const int dy4[4] = {0, -1, 1, 0};
const int inf = 0x3f3f3f3f;
const int N = 1E5 + 7;
int a[N], pre[N], lst[N];
int n, m;
int tree[N << 2];
int t[N];
int ans[N];
struct node {
  int l, r;
  int id;
  bool operator<(node b) const {
    if (r == b.r) return l < b.l;
    return r < b.r;
  }
} q[N];
void PushUp(int rt) { tree[rt] = tree[rt << 1] + tree[rt << 1 | 1]; }
void update(int p, int sc, int l, int r, int rt) {
  if (l == r) {
    tree[rt] += sc;
    return;
  }
  int m = (l + r) >> 1;
  if (p <= m)
    update(p, sc, l, m, rt << 1);
  else
    update(p, sc, m + 1, r, rt << 1 | 1);
  PushUp(rt);
}
int query(int L, int R, int l, int r, int rt) {
  if (L <= l && r <= R) return tree[rt];
  int m = (l + r) >> 1;
  int ret = 0;
  if (L <= m) ret += query(L, R, l, m, rt << 1);
  if (R >= m + 1) ret += query(L, R, m + 1, r, rt << 1 | 1);
  return ret;
}
int lowbit(int x) { return x & (-x); }
void update2(int x, int delta) {
  for (int i = x; i < N; i += lowbit(i)) t[i] += delta;
}
int Sum(int x) {
  int res = 0;
  for (int i = x; i >= 1; i -= lowbit(i)) res += t[i];
  return res;
}
int main() {
  cin >> n;
  memset(tree, 0, sizeof(tree));
  memset(t, 0, sizeof(t));
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    pre[i] = lst[a[i]];
    lst[a[i]] = i;
  }
  cin >> m;
  for (int i = 1; i <= m; i++) {
    cin >> q[i].l >> q[i].r;
    q[i].id = i;
  }
  sort(q + 1, q + m + 1);
  int cur = 1;
  for (int i = 1; i <= m; i++) {
    for (; cur <= q[i].r; cur++) {
      if (pre[cur]) update(pre[cur], -1, 1, n, 1);
      update(cur, 1, 1, n, 1);
      update2(pre[cur] + 1, 1);
      update2(cur + 1, -1);
      if (pre[cur]) {
        int k = pre[cur];
        if (!pre[k] || k - pre[k] == cur - k)
          ;
        else {
          int len = k - pre[k];
          k = pre[k];
          for (; k;) {
            int A = pre[k];
            update2(A + 1, -1);
            update2(k + 1, 1);
            if (!A || k - A != len) break;
            k = A;
          }
        }
      }
    }
    ans[q[i].id] = query(q[i].l, q[i].r, 1, n, 1) + (Sum(q[i].l) == 0);
  }
  for (int i = 1; i <= m; i++) cout << ans[i] << endl;
  return 0;
}
