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 int maxn = 100010;
int n, a[maxn], pre[maxn], pos[maxn], ans[maxn], Q, dis[maxn], yuan[maxn], maxx,
    c[maxn];
int tag[maxn << 2], sum[maxn << 2], L[maxn << 2], R[maxn << 2];
vector<pair<int, int> > V[maxn];
void pushdown(int o) {
  if (tag[o]) {
    tag[o * 2] += tag[o];
    tag[o * 2 + 1] += tag[o];
    sum[o * 2] += tag[o];
    sum[o * 2 + 1] += tag[o];
    tag[o] = 0;
  }
}
void build(int o, int l, int r) {
  L[o] = l, R[o] = r;
  tag[o] = 0;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build(o * 2, l, mid);
  build(o * 2 + 1, mid + 1, r);
}
void update(int o, int l, int r, int x, int y, int v) {
  if (x > y || y < l || x > r) return;
  if (x <= l && r <= y) {
    sum[o] += v;
    tag[o] += v;
    return;
  }
  pushdown(o);
  int mid = (l + r) >> 1;
  if (x <= mid) update(o * 2, l, mid, x, y, v);
  if (y > mid) update(o * 2 + 1, mid + 1, r, x, y, v);
}
int query(int o, int l, int r, int pos) {
  if (l == r) return sum[o];
  pushdown(o);
  int mid = (l + r) >> 1;
  if (pos <= mid)
    return query(o * 2, l, mid, pos);
  else
    return query(o * 2 + 1, mid + 1, r, pos);
}
void gai(int x, int opt) {
  if (x == -1) return;
  if (pre[yuan[x]] == -1)
    update(1, 1, n, 1, x, opt);
  else
    update(1, 1, n, pre[yuan[x]] + 1, x, opt);
}
namespace seg {
int L[maxn << 2], R[maxn << 2], sum[maxn << 2];
void pushup(int o) { sum[o] = sum[o * 2] + sum[o * 2 + 1]; }
void build(int o, int l, int r) {
  L[o] = l, R[o] = r;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build(o * 2, l, mid);
  build(o * 2 + 1, mid + 1, r);
}
void update(int o, int l, int r, int pos, int v) {
  if (l == r) {
    sum[o] = v;
    return;
  }
  int mid = (l + r) >> 1;
  if (pos <= mid)
    update(o * 2, l, mid, pos, v);
  else
    update(o * 2 + 1, mid + 1, r, pos, v);
  pushup(o);
}
int query(int o, int l, int r, int x, int y) {
  if (x <= l && r <= y) return sum[o];
  int mid = (l + r) >> 1;
  if (y <= mid)
    return query(o * 2, l, mid, x, y);
  else if (x > mid)
    return query(o * 2 + 1, mid + 1, r, x, y);
  else
    return query(o * 2, l, mid, x, mid) +
           query(o * 2 + 1, mid + 1, r, mid + 1, y);
}
}  // namespace seg
int main() {
  memset(pos, -1, sizeof(pos));
  memset(pre, -1, sizeof(pre));
  memset(c, -1, sizeof(c));
  memset(dis, -1, sizeof(dis));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    pre[i] = pos[a[i]];
    pos[a[i]] = i;
  }
  scanf("%d", &Q);
  for (int i = 1; i <= Q; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    V[y].push_back(make_pair(x, i));
  }
  build(1, 1, n);
  seg::build(1, 1, n);
  for (int i = 1; i <= n; i++) {
    if (pre[i] == -1)
      update(1, 1, n, 1, i, 1);
    else {
      if (dis[pre[i]] == -1) {
        dis[i] = i - pre[i];
        yuan[i] = pre[i];
      } else {
        if (dis[pre[i]] == i - pre[i])
          yuan[i] = yuan[pre[i]];
        else
          yuan[i] = pre[i];
        dis[i] = i - pre[i];
      }
      gai(pre[i], -1);
      gai(i, 1);
    }
    if (c[a[i]] != -1) seg::update(1, 1, n, c[a[i]], 0);
    c[a[i]] = i;
    seg::update(1, 1, n, i, 1);
    for (int j = 0; j < V[i].size(); j++) {
      int x = V[i][j].first, y = V[i][j].second;
      int temp = query(1, 1, n, x);
      int temp2 = seg::query(1, 1, n, x, i);
      if (temp)
        ans[y] = temp2;
      else
        ans[y] = temp2 + 1;
    }
  }
  for (int i = 1; i <= Q; i++) printf("%d\n", ans[i]);
  return 0;
}
