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 Question {
  int l, r;
  int num;
} question[100005];
deque<int> dq[100005];
int n, m, s[100005];
int t[100005], cnt;
int siz;
int ans[100005];
int able;
bool can[100005];
vector<int> v[100005];
int r[100005];
void update(int x, bool flag, int flag2) {
  if (flag) {
    if (flag2 == -1) {
      if (dq[s[x]].size() == 0) {
        dq[s[x]].push_front(x);
        can[s[x]] = true;
        ++able;
      } else {
        dq[s[x]].push_front(x);
        if (dq[s[x]].size() > 2 && can[s[x]] && r[dq[s[x]].back()] != r[x] &&
            r[x] != dq[s[x]].back()) {
          can[s[x]] = false;
          --able;
        }
      }
    } else {
      if (dq[s[x]].size() == 0) {
        dq[s[x]].push_back(x);
        can[s[x]] = true;
        ++able;
      } else {
        dq[s[x]].push_back(x);
        if (dq[s[x]].size() > 2 && can[s[x]] && r[dq[s[x]].front()] != r[x] &&
            r[dq[s[x]].front()] != x) {
          can[s[x]] = false;
          --able;
        }
      }
    }
    if (t[s[x]] == 0) ++cnt;
    ++t[s[x]];
  } else {
    if (flag2 == -1) {
      dq[s[x]].pop_front();
    } else {
      dq[s[x]].pop_back();
    }
    if (dq[s[x]].size() == 0) {
      can[s[x]] = false;
      --able;
    } else {
      if (!can[s[x]] && (r[dq[s[x]].back()] == r[dq[s[x]].front()] ||
                         r[dq[s[x]].front()] == dq[s[x]].back())) {
        can[s[x]] = true;
        ++able;
      }
    }
    --t[s[x]];
    if (t[s[x]] == 0) --cnt;
  }
}
bool cmp(Question a, Question b) {
  int x = a.l / siz, y = b.l / siz;
  if (x == y) {
    if (x & 1)
      return a.r < b.r;
    else
      return a.r > b.r;
  } else
    return x < y;
}
int main() {
  clock_t start = clock();
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> s[i];
    v[s[i]].push_back(i);
  }
  for (int i = 1; i <= 100000; ++i) {
    if (v[i].size() == 0) continue;
    if (v[i].size() == 1) {
      r[v[i][0]] = v[i][0];
      continue;
    }
    int j = 0, len = v[i][1] - v[i][0];
    for (int k = 1; k < v[i].size() - 1; ++k) {
      if (v[i][k + 1] - v[i][k] != len) {
        for (int x = k - 1; x >= j; --x) r[v[i][x]] = v[i][k];
        j = k;
        len = v[i][k + 1] - v[i][k];
      }
    }
    for (int x = v[i].size() - 1; x >= j; --x)
      r[v[i][x]] = v[i][v[i].size() - 1];
  }
  cin >> m;
  siz = sqrt(m);
  for (int i = 1; i <= m; ++i) {
    cin >> question[i].l >> question[i].r;
    question[i].num = i;
  }
  sort(question + 1, question + m + 1, cmp);
  int l = 1, r = 0;
  for (int i = 1; i <= m; ++i) {
    while (r < question[i].r) update(++r, 1, 1);
    while (l > question[i].l) update(--l, 1, -1);
    while (r > question[i].r) update(r--, 0, 1);
    while (l < question[i].l) update(l++, 0, -1);
    ans[question[i].num] = cnt + 1 - (able > 0);
  }
  for (int i = 1; i <= m; ++i) cout << ans[i] << '\n';
  return 0;
}
