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 = 1e5 + 10;
int n;
struct DS {
  vector<int> seg[Max * 4];
  vector<int> A[Max];
  void build(int tl = 0, int tr = n + 2, int idx = 1) {
    if (tr - tl == 1) {
      seg[idx] = A[tl];
      return;
    }
    int mid = (tl + tr) / 2;
    build(tl, mid, idx * 2);
    build(mid, tr, idx * 2 + 1);
    merge(seg[idx * 2].begin(), seg[idx * 2].end(), seg[idx * 2 + 1].begin(),
          seg[idx * 2 + 1].end(), back_inserter(seg[idx]));
  }
  int get(int l, int r, int tl = 0, int tr = n + 2, int idx = 1) {
    if (l <= tl && tr <= r) {
      if (seg[idx].size() == 0 || seg[idx][0] >= r) return 0;
      int n = seg[idx].size(), y = 0;
      while (n - y > 1) {
        int mid = (n + y) / 2;
        if (seg[idx][mid] < r)
          y = mid;
        else
          n = mid;
      }
      return y + 1;
    }
    if (tr <= l || r <= tl) return 0;
    int mid = (tl + tr) / 2;
    return get(l, r, tl, mid, idx * 2) + get(l, r, mid, tr, idx * 2 + 1);
  }
};
struct DS2 {
  vector<int> seg[Max * 4];
  vector<int> A[Max];
  void build(int tl = 1, int tr = n + 5, int idx = 1) {
    if (tr - tl == 1) {
      seg[idx] = A[tl];
      return;
    }
    int mid = (tl + tr) / 2;
    build(tl, mid, idx * 2);
    build(mid, tr, idx * 2 + 1);
    merge(seg[idx * 2].begin(), seg[idx * 2].end(), seg[idx * 2 + 1].begin(),
          seg[idx * 2 + 1].end(), back_inserter(seg[idx]));
  }
  int get(int l, int r, int tl = 1, int tr = n + 5, int idx = 1) {
    if (tr - 1 <= l) {
      if (seg[idx].size() == 0 || seg[idx][0] > r) return 0;
      int n = seg[idx].size(), y = 0;
      while (n - y > 1) {
        int mid = (n + y) / 2;
        if (seg[idx][mid] <= r)
          y = mid;
        else
          n = mid;
      }
      return y + 1;
    }
    if (tl > l) return 0;
    int mid = (tl + tr) / 2;
    return get(l, r, tl, mid, idx * 2) + get(l, r, mid, tr, idx * 2 + 1);
  }
};
DS typecount;
DS2 neg;
DS2 pos;
int lastX[Max];
int bck[Max];
int nxt[Max];
int lastok[Max];
int A[Max];
int main() {
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n + 1; i++) nxt[i] = n + 1;
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
    bck[i] = lastX[A[i]];
    nxt[lastX[A[i]]] = i;
    lastX[A[i]] = i;
  }
  for (int i = n; i >= 1; i--) {
    if (nxt[i] == n + 1 || nxt[nxt[i]] == n + 1)
      lastok[i] = n + 1;
    else if (nxt[i] - i == nxt[nxt[i]] - nxt[i])
      lastok[i] = lastok[nxt[i]];
    else
      lastok[i] = nxt[nxt[i]];
  }
  for (int i = 1; i <= n; i++) {
    typecount.A[bck[i]].push_back(i);
    pos.A[bck[i] + 1].push_back(i);
    neg.A[i + 1].push_back(i);
    neg.A[bck[i] + 1].push_back(lastok[i]);
    pos.A[i + 1].push_back(lastok[i]);
  }
  for (int i = 1; i <= n; i++) {
    sort(pos.A[i].begin(), pos.A[i].end());
    sort(neg.A[i].begin(), neg.A[i].end());
  }
  typecount.build();
  pos.build();
  neg.build();
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    int tc = r - l + 1 - typecount.get(l, r + 1);
    int isgood = pos.get(l, r);
    isgood -= neg.get(l, r);
    if (isgood == 0)
      cout << (tc + 1) << '\n';
    else
      cout << tc << '\n';
  }
}
