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 ques {
  int l, r, segment, ques_num;
  bool operator<(ques other) const {
    if (segment < other.segment)
      return 1;
    else if (segment == other.segment and r < other.r)
      return 1;
    else
      return 0;
  }
};
int m, b[110000], q, l, r, currans = 0, count1[110000], ans[110000],
                           prev1[110000], continuous = 0, next1[110000],
                           left1[110000], right1[110000], consd[110000];
ques queries[110000];
vector<map<int, int> > arr(110000);
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> m;
  for (int i = 1; i <= m; i++) cin >> b[i];
  cin >> q;
  int sqrtn = sqrt(m);
  for (int i = 0; i < q; i++) {
    cin >> l >> r;
    queries[i].l = l;
    queries[i].r = r;
    queries[i].ques_num = i;
    queries[i].segment = l / sqrtn;
  }
  sort(queries, queries + q);
  int lf = 1, rg = 0;
  for (int i = 0; i < q; i++) {
    l = queries[i].l, r = queries[i].r;
    int num = queries[i].ques_num;
    while (r > rg) {
      rg++;
      int ind1 = b[rg];
      count1[ind1]++;
      if (count1[ind1] == 1) {
        currans++;
        continuous++;
        consd[ind1] = 1;
        left1[ind1] = right1[ind1] = rg;
      } else {
        int diff = rg - right1[ind1];
        if (arr[ind1].count(diff) < 1)
          arr[ind1][diff] = 1;
        else
          arr[ind1][diff]++;
        prev1[rg] = right1[ind1];
        next1[right1[ind1]] = rg;
        right1[ind1] = rg;
      }
      if (consd[ind1] == 1) {
        if (arr[ind1].size() > 1) {
          consd[ind1] = 0;
          continuous--;
        }
      }
    }
    while (l < lf) {
      lf--;
      int ind1 = b[lf];
      count1[ind1]++;
      if (count1[ind1] == 1) {
        currans++;
        continuous++;
        consd[ind1] = 1;
        left1[ind1] = right1[ind1] = lf;
      } else {
        int diff = left1[ind1] - lf;
        if (arr[ind1].count(diff) < 1)
          arr[ind1][diff] = 1;
        else
          arr[ind1][diff]++;
        next1[lf] = left1[ind1];
        prev1[left1[ind1]] = lf;
        left1[ind1] = lf;
      }
      if (consd[ind1] == 1) {
        if (arr[ind1].size() > 1) {
          consd[ind1] = 0;
          continuous--;
        }
      }
    }
    while (l > lf) {
      int ind1 = b[lf];
      count1[ind1]--;
      if (count1[ind1] == 0) {
        currans--;
        if (consd[ind1]) continuous--;
      } else {
        int diff = next1[lf] - lf;
        left1[ind1] = next1[lf];
        arr[ind1][diff]--;
        if (arr[ind1][diff] == 0) arr[ind1].erase(diff);
      }
      if (consd[ind1] == 0 and count1[ind1] >= 1) {
        if (arr[ind1].size() <= 1) {
          consd[ind1] = 1;
          continuous++;
        }
      }
      lf++;
    }
    while (r < rg) {
      int ind1 = b[rg];
      count1[ind1]--;
      if (count1[ind1] == 0) {
        currans--;
        if (consd[ind1]) continuous--;
      } else {
        int diff = rg - prev1[rg];
        right1[ind1] = prev1[rg];
        arr[ind1][diff]--;
        if (arr[ind1][diff] == 0) arr[ind1].erase(diff);
      }
      if (consd[ind1] == 0 and count1[ind1] >= 1) {
        if (arr[ind1].size() <= 1) {
          consd[ind1] = 1;
          continuous++;
        }
      }
      rg--;
    }
    if (continuous >= 1)
      ans[num] = currans;
    else
      ans[num] = currans + 1;
  }
  for (int i = 0; i < q; i++) cout << ans[i] << "\n";
}
