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 MAXM = 1e5 + 66.6;
int arr[MAXM];
map<int, int> dis[MAXM];
deque<int> de[MAXM];
;
bool ok[MAXM];
pair<int, int> Q[MAXM];
int ans[MAXM];
int cur = 0, curans = 0, SQ;
void add(int x) {
  if (de[arr[x]].size()) {
    int dif;
    if (x > de[arr[x]].front())
      dif = x - de[arr[x]].back(), de[arr[x]].push_back(x);
    else
      dif = de[arr[x]].front() - x, de[arr[x]].push_front(x);
    dis[arr[x]][dif]++;
    if (dis[arr[x]].size() == 1)
      ok[arr[x]] = true;
    else if (ok[arr[x]])
      cur--, ok[arr[x]] = false;
  } else {
    de[arr[x]].push_back(x);
    ok[arr[x]] = true;
    cur++;
    curans++;
  }
}
void del(int x) {
  int s;
  if (x == de[arr[x]].front()) {
    de[arr[x]].pop_front();
    if (de[arr[x]].size()) {
      dis[arr[x]][s = de[arr[x]].front() - x]--;
      if (dis[arr[x]][s] == 0) dis[arr[x]].erase(s);
    }
  } else {
    de[arr[x]].pop_back();
    if (de[arr[x]].size()) {
      dis[arr[x]][s = x - de[arr[x]].back()]--;
      if (dis[arr[x]][s] == 0) dis[arr[x]].erase(s);
    }
  }
  if (de[arr[x]].size() == 0) {
    curans--;
    cur--;
    ok[arr[x]] = false;
  } else if (de[arr[x]].size() > 1 && dis[arr[x]].size() == 1) {
    cur += !ok[arr[x]];
    ok[arr[x]] = true;
  }
}
bool comp(int x, int y) {
  return (Q[x].first / SQ == Q[y].first / SQ ? Q[x].second < Q[y].second
                                             : Q[x].first < Q[y].first);
}
int main() {
  int m;
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) scanf("%d", arr + i);
  int q;
  scanf("%d", &q);
  SQ = sqrt(m);
  int queries[q];
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &Q[i].first, &Q[i].second);
    queries[i] = i;
  }
  sort(queries, queries + q, comp);
  int l = 0, r = 0;
  add(0);
  for (auto u : queries) {
    while (r < Q[u].second) add(++r);
    while (l > Q[u].first) add(--l);
    while (l < Q[u].first) del(l++);
    while (r > Q[u].second) del(r--);
    ans[u] = curans + (cur == 0);
  }
  for (int i = 0; i < q; i++) printf("%d\n", ans[i]);
  return 0;
}
