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>
#pragma warning(disable : 4996)
using namespace std;
struct qry {
  int l, r, id, val;
  int block;
};
struct custom_struct {
  int cnt;
  bool in_ap;
  deque<int> pos;
  deque<int> diff;
  custom_struct() : cnt(0), in_ap(false), diff(0) {}
};
int b_search(int X) {
  int S = 0, E = X;
  long long mid;
  while (S < E) {
    mid = (S + E) >> 1;
    if (mid * mid >= X)
      E = mid;
    else
      S = mid + 1;
  }
  return E;
}
class MoAlgo {
  int n, q;
  vector<int> data;
  vector<int> ans;
  vector<int> q_idx;
  vector<qry> Q;

 public:
  bool operator()(const int &x, const int &y) const {
    if (Q[x].block == Q[y].block) return Q[x].r < Q[y].r;
    return Q[x].block < Q[y].block;
  }
  MoAlgo(int N = 0) : n(N), data(N) {
    int bs = b_search(N);
    for (auto &x : data) {
      scanf("%d", &x);
      --x;
    }
    scanf("%d", &q);
    Q.resize(q);
    q_idx.resize(q);
    ans.resize(q);
    for (int i = 0; i < q; i++) {
      int u;
      scanf("%d%d", &Q[i].l, &Q[i].r);
      --Q[i].l, --Q[i].r;
      Q[i].block = Q[i].l / bs;
      Q[i].id = i;
      q_idx[i] = i;
    }
    sort(q_idx.begin(), q_idx.end(),
         [this](int x, int y) { return (*this)(x, y); });
    vector<custom_struct> v(1e5);
    int in_ap = 0, distinct_n = 0;
    int L = 0, R = 0;
    v[data[0]].pos.push_back(0);
    v[data[0]].cnt = 1;
    v[data[0]].in_ap = true;
    distinct_n = 1;
    in_ap = 1;
    for (auto &x : q_idx) {
      int left = Q[x].l, right = Q[x].r, id = Q[x].id;
      while (R > right) {
        int c = data[R], f = v[c].pos.back();
        v[c].pos.pop_back();
        v[c].cnt--;
        if (v[c].cnt == 0) in_ap--, v[c].in_ap = 0, distinct_n--;
        if (v[c].cnt < 2) {
          if (!v[c].diff.empty()) v[c].diff.pop_back();
        } else {
          bool preq = v[c].in_ap;
          if (v[c].pos.back() - v[c].pos[v[c].cnt - 2] != v[c].diff.back())
            v[c].diff.pop_back();
          if (v[c].diff.size() == 1) v[c].in_ap = true;
          if (preq == false && v[c].in_ap == true) in_ap++;
        }
        R--;
      }
      while (R < right) {
        ++R;
        int c = data[R];
        v[c].pos.push_back(R);
        v[c].cnt++;
        if (v[c].cnt == 1)
          v[c].in_ap = true, ++in_ap, distinct_n++;
        else if (v[c].cnt == 2) {
          v[c].diff.push_back(v[c].pos[1] - v[c].pos[0]);
        } else {
          if (v[c].pos[v[c].cnt - 1] - v[c].pos[v[c].cnt - 2] !=
              v[c].diff.back())
            v[c].diff.push_back(v[c].pos[v[c].cnt - 1] -
                                v[c].pos[v[c].cnt - 2]);
          bool prev = v[c].in_ap;
          if (v[c].diff.size() > 1) v[c].in_ap = false;
          if (prev == true && v[c].in_ap == false) --in_ap;
        }
      }
      while (L < left) {
        int c = data[L], f = v[c].pos.front();
        v[c].pos.pop_front();
        v[c].cnt--;
        if (v[c].cnt == 0) in_ap--, v[c].in_ap = 0, distinct_n--;
        if (v[c].cnt < 2) {
          if (!v[c].diff.empty()) v[c].diff.pop_back();
        } else {
          bool preq = v[c].in_ap;
          if (v[c].pos[1] - v[c].pos[0] != v[c].diff.front())
            v[c].diff.pop_front();
          if (v[c].diff.size() == 1) v[c].in_ap = true;
          if (preq == false && v[c].in_ap == true) in_ap++;
        }
        L++;
      }
      while (L > left) {
        --L;
        int c = data[L];
        v[c].pos.push_front(L);
        v[c].cnt++;
        if (v[c].cnt == 1)
          v[c].in_ap = true, ++in_ap, distinct_n++;
        else if (v[c].cnt == 2) {
          v[c].diff.push_front(v[c].pos[1] - v[c].pos[0]);
        } else {
          if (v[c].pos[1] - v[c].pos[0] != v[c].diff.front())
            v[c].diff.push_front(v[c].pos[1] - v[c].pos[0]);
          bool prev = v[c].in_ap;
          if (v[c].diff.size() > 1) v[c].in_ap = false;
          if (prev == true && v[c].in_ap == false) --in_ap;
        }
      }
      if (in_ap)
        ans[id] = distinct_n;
      else
        ans[id] = distinct_n + 1;
    }
    for (int i = 0; i < q; i++) printf("%d\n", ans[i]);
  }
};
int main() {
  int n;
  scanf("%d", &n);
  new MoAlgo(n);
  return 0;
}
