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 N = 1e5 + 100, SQ = 320;
int n, m, l, r, a[N], _SQ[N], OK, CNT, ans[N];
vector<pair<pair<int, int>, int> > q;
struct node {
  int dif_cnt;
  deque<int> q;
  unordered_map<int, int> cnt;
  void add(int a) {
    if (cnt[a] == 0) {
      if (dif_cnt == 1) OK--;
      dif_cnt++;
    }
    cnt[a]++;
  }
  void del(int a) {
    if (cnt[a] == 1) {
      if (dif_cnt == 2) OK++;
      dif_cnt--;
    }
    cnt[a]--;
  }
  void pop_back() {
    int dex = q.back();
    q.pop_back();
    if (q.size())
      (*this).del(dex - q.back());
    else {
      CNT--;
      OK--;
    }
  }
  void pop_front() {
    int dex = q.front();
    q.pop_front();
    if (q.size())
      (*this).del(q.front() - dex);
    else {
      CNT--;
      OK--;
    }
  }
  void push_back(int dex) {
    if (q.size())
      (*this).add(dex - q.back());
    else {
      CNT++;
      OK++;
    }
    q.push_back(dex);
  }
  void push_front(int dex) {
    if (q.size())
      (*this).add(q.front() - dex);
    else {
      CNT++;
      OK++;
    }
    q.push_front(dex);
  }
} s[N];
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  for (int i = 0; i < N; i++) _SQ[i] = i / SQ;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  cin >> m;
  for (int i = 0; i < m; i++) {
    pair<pair<int, int>, int> help;
    cin >> help.first.first >> help.first.second;
    help.first.first--;
    help.second = i;
    q.push_back(help);
  }
  sort(q.begin(), q.end(),
       [](pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
         if (_SQ[a.first.first] != _SQ[b.first.first])
           return a.first.first < b.first.first;
         return a.first.second < b.first.second;
       });
  int l = 0, r = 0;
  for (pair<pair<int, int>, int> p : q) {
    while (l > p.first.first) s[a[--l]].push_front(l);
    while (r < p.first.second) {
      s[a[r]].push_back(r);
      r++;
    }
    while (l < p.first.first) {
      s[a[l]].pop_front();
      l++;
    }
    while (r > p.first.second) s[a[--r]].pop_back();
    ans[p.second] = CNT - (OK > 0) + 1;
  }
  for (int i = 0; i < m; i++) cout << ans[i] << '\n';
  return 0;
}
