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 long long N = 1e5 + 9, B = 320;
long long a[N], len[N], d[N], ans[N], num = 0, res = 0;
pair<pair<long long, long long>, long long> qry[N];
deque<long long> deq[N];
bool CMP(pair<pair<long long, long long>, long long> i,
         pair<pair<long long, long long>, long long> j) {
  if (i.first.first / B == j.first.first / B)
    return (i.first.second < j.first.second);
  return ((i.first.first / B) < (j.first.first / B));
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> a[i];
  long long q;
  cin >> q;
  for (long long i = 0; i < q; i++)
    cin >> qry[i].first.first >> qry[i].first.second, qry[i].first.first--,
        qry[i].second = i;
  sort(qry, qry + q, CMP);
  long long l = 0, r = 0;
  for (long long i = 0; i < q; i++) {
    pair<long long, long long> p = qry[i].first;
    if (i > 0 && qry[i - 1].first.second > p.second) {
      for (long long j = 0; j < N; j++) deq[j].clear();
      fill(len, len + N, 0);
      fill(d, d + N, -1);
      l = 0, r = 0, num = res = 0;
    }
    while (r < p.second) {
      long long x = a[r];
      if (deq[x].empty()) {
        res++;
        num++;
        deq[x].push_back(r);
        len[x] = 1;
      } else if (deq[x].size() == 1) {
        d[x] = r - deq[x].back();
        deq[x].push_back(r);
        len[x] = 2;
      } else {
        if (d[x] == r - deq[x].back()) {
          deq[x].push_back(r);
          len[x]++;
        } else {
          if (len[x] == deq[x].size()) res--;
          d[x] = r - deq[x].back();
          deq[x].push_back(r);
          len[x] = 2;
        }
      }
      r++;
    }
    while (l < p.first) {
      long long x = a[l];
      if (deq[x].size() == 1) {
        res--;
        num--;
        deq[x].pop_front();
        len[x] = 0;
      } else if (deq[x].size() == 2) {
        d[x] = -1;
        deq[x].pop_front();
        len[x] = 1;
      } else {
        if (len[x] + 1 == deq[x].size()) res++;
        if (len[x] == deq[x].size()) len[x]--;
        deq[x].pop_front();
      }
      l++;
    }
    while (l > p.first) {
      l--;
      long long x = a[l];
      if (deq[x].size() == 0) {
        res++;
        num++;
        deq[x].push_front(l);
        len[x] = 1;
      } else if (deq[x].size() == 1) {
        d[x] = deq[x].front() - l;
        deq[x].push_front(l);
        len[x] = 2;
      } else {
        if (deq[x].size() == len[x]) {
          if (d[x] == deq[x].front() - l)
            len[x]++;
          else
            res--;
        }
        deq[x].push_front(l);
      }
    }
    ans[qry[i].second] = num + 1 - (res > 0);
  }
  for (long long i = 0; i < q; i++) cout << ans[i] << '\n';
  return 0;
}
