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;
struct Query {
  int l, r, num;
};
const int max_n = 100500;
int fen[max_n];
void add(int i, int d) {
  for (; i < max_n; i |= i + 1) {
    fen[i] += d;
  }
}
int sum(int i) {
  int ans = 0;
  for (; i >= 0; i = (i & (i + 1)) - 1) {
    ans += fen[i];
  }
  return ans;
}
int sum(int l, int r) { return sum(r) - sum(l - 1); }
int t[max_n << 2], d = 1;
void build() {
  while (d <= max_n) d <<= 1;
  for (int i = 0; i < (max_n << 2); i++) {
    t[i] = INT_MAX;
  }
}
void update(int i, int x, int tl = 0, int tr = d - 1, int v = 1) {
  if (tl == tr) {
    t[v] = x;
    return;
  }
  int tm = (tl + tr) / 2;
  if (i <= tm)
    update(i, x, tl, tm, v << 1);
  else
    update(i, x, tm + 1, tr, v << 1 | 1);
  t[v] = min(t[v << 1], t[v << 1 | 1]);
}
int get(int l, int r, int tl = 0, int tr = d - 1, int v = 1) {
  if (l == tl && r == tr) {
    return t[v];
  }
  int tm = (tl + tr) / 2;
  int ans = INT_MAX;
  if (l <= tm) {
    ans = min(ans, get(l, min(tm, r), tl, tm, v << 1));
  }
  if (r > tm) {
    ans = min(ans, get(max(tm + 1, l), r, tm + 1, tr, v << 1 | 1));
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  build();
  int n;
  cin >> n;
  vector<int> v(n);
  for (int &e : v) {
    cin >> e;
  }
  int q;
  cin >> q;
  vector<Query> queries(q);
  for (int i = 0; i < q; i++) {
    cin >> queries[i].l >> queries[i].r;
    queries[i].l--;
    queries[i].r--;
    queries[i].num = i;
  }
  sort(queries.begin(), queries.end(),
       [](Query a, Query b) { return a.r < b.r; });
  int j = 0;
  vector<int> last(max_n, -1);
  vector<int> lastPos(n);
  vector<int> step(max_n, 0);
  vector<int> prev(max_n, 0);
  vector<int> ans(q);
  for (Query i : queries) {
    while (j <= i.r) {
      if (last[v[j]] != -1) {
        add(last[v[j]], -1);
        if (j - last[v[j]] == step[v[j]]) {
          update(j, get(last[v[j]], last[v[j]]));
        } else {
          update(j, lastPos[last[v[j]]]);
        }
        step[v[j]] = j - last[v[j]];
        update(last[v[j]], INT_MAX);
        lastPos[j] = last[v[j]];
      } else {
        update(j, -1);
        lastPos[j] = -1;
      }
      add(j, 1);
      last[v[j]] = j;
      j++;
    }
    int cnt = sum(i.l, i.r);
    if (get(i.l, i.r) >= i.l) {
      cnt++;
    }
    ans[i.num] = cnt;
  }
  for (int i : ans) {
    cout << i << "\n";
  }
}
