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 MAX_SIZE = 100000;
const int INF = 0x3f3f3f3f;
struct Query {
  int l, r;
  int id;
};
inline bool operator<(const Query& a, const Query& b) { return a.r < b.r; }
int arr[MAX_SIZE + 10];
inline int lowbit(int a) { return a & -a; }
void change(int p, int inc) {
  for (; p <= MAX_SIZE; p += lowbit(p)) arr[p] += inc;
}
int query(int p) {
  int ans = 0;
  for (; p; p -= lowbit(p)) ans += arr[p];
  return ans;
}
inline void readInt(int& a) {
  a = 0;
  int c;
  do c = getchar();
  while (c < '0' || c > '9');
  do {
    a = a * 10 - '0' + c;
    c = getchar();
  } while (c >= '0' && c <= '9');
}
struct Node {
  int l, r;
  Node *left, *right;
  int minn;
};
int data[MAX_SIZE + 10], n, m;
Query qu[MAX_SIZE + 10];
int pre[MAX_SIZE + 10];
int ans[MAX_SIZE + 10], plen[MAX_SIZE + 10], gap[MAX_SIZE + 10];
Node tree[MAX_SIZE * 2 + 10], *root;
int tn;
inline int min(int a, int b) { return a < b ? a : b; }
Node* build(int l, int r) {
  Node* cur = tree + (tn++);
  cur->l = l;
  cur->r = r;
  cur->minn = INF;
  if (l != r) {
    int mid = (l + r) >> 1;
    cur->left = build(l, mid);
    cur->right = build(mid + 1, r);
  }
  return cur;
}
void change(Node* cur, int p, int val) {
  if (cur->l == cur->r) {
    cur->minn = val;
    return;
  }
  int mid = (cur->l + cur->r) >> 1;
  if (p <= mid)
    change(cur->left, p, val);
  else
    change(cur->right, p, val);
  cur->minn = min(cur->left->minn, cur->right->minn);
}
int query(Node* cur, int l, int r) {
  if (cur->l == l && cur->r == r) return cur->minn;
  int mid = (cur->l + cur->r) >> 1;
  if (r <= mid)
    return query(cur->left, l, r);
  else if (l > mid)
    return query(cur->right, l, r);
  else
    return min(query(cur->left, l, mid), query(cur->right, mid + 1, r));
}
int main() {
  readInt(n);
  for (int i = 1; i <= n; i++) readInt(data[i]);
  readInt(m);
  for (int i = 1; i <= m; i++) {
    readInt(qu[i].l);
    readInt(qu[i].r);
    qu[i].id = i;
  }
  sort(qu + 1, qu + m + 1);
  root = build(1, MAX_SIZE);
  int cnt = 0, p = 0;
  for (int i = 1; i <= m; i++) {
    while (p < qu[i].r) {
      ++p;
      if (pre[data[p]]) {
        change(pre[data[p]], -1);
        change(p, 1);
        change(root, pre[data[p]], INF);
        if (gap[data[p]] == 0 || p - pre[data[p]] == gap[data[p]])
          plen[data[p]] += p - pre[data[p]];
        else
          plen[data[p]] = p - pre[data[p]] + gap[data[p]] - 1;
        gap[data[p]] = p - pre[data[p]];
        change(root, p, p - plen[data[p]]);
      } else {
        change(p, 1);
        change(root, p, 1);
        plen[data[p]] = p - 1;
        ++cnt;
      }
      pre[data[p]] = p;
    }
    ans[qu[i].id] =
        cnt - query(qu[i].l - 1) + (query(root, qu[i].l, qu[i].r) > qu[i].l);
  }
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
  return 0;
}
