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;
template <typename T, typename U>
inline void smin(T &a, U b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
  if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
  char c, sg = 0;
  while (c = getchar(), (c > '9' || c < '0') && c != '-')
    ;
  for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
       c = getchar())
    first = (first << 1) + (first << 3) + c - '0';
  if (sg) first = -first;
}
template <class T, class T1>
inline void gn(T &first, T1 &second) {
  gn(first);
  gn(second);
}
template <class T, class T1, class T2>
inline void gn(T &first, T1 &second, T2 &z) {
  gn(first);
  gn(second);
  gn(z);
}
template <class T>
inline void print(T first) {
  if (first < 0) {
    putchar('-');
    return print(-first);
  }
  if (first < 10) {
    putchar('0' + first);
    return;
  }
  print(first / 10);
  putchar(first % 10 + '0');
}
template <class T>
inline void printsp(T first) {
  print(first);
  putchar(' ');
}
template <class T>
inline void println(T first) {
  print(first);
  putchar('\n');
}
template <class T, class U>
inline void print(T first, U second) {
  printsp(first);
  println(second);
}
template <class T, class U, class V>
inline void print(T first, U second, V z) {
  printsp(first);
  printsp(second);
  println(z);
}
int power(int a, int b, int m, int ans = 1) {
  for (; b; b >>= 1, a = 1LL * a * a % m)
    if (b & 1) ans = 1LL * ans * a % m;
  return ans;
}
int S;
pair<pair<int, int>, int> q[100011];
int b[100011];
deque<int> dq[100011];
int vst[100011];
int nxt_same[100011];
int nxt[100011];
int pos[100011];
int good[100011];
bool cmp(pair<pair<int, int>, int> u, pair<pair<int, int>, int> v) {
  if (u.first.first / S == v.first.first / S)
    return u.first.second < v.first.second;
  return u.first.first < v.first.first;
}
int ans[100011];
int cnt;
int ar;
void poke(int u, int d, int dir) {
  int v = b[u];
  if (d == 1) {
    if (vst[v] == 0) cnt++;
    vst[v]++;
    ar -= good[v];
    if (dir == 1)
      dq[v].push_back(u);
    else
      dq[v].push_front(u);
    if (nxt[dq[v].front()] >= dq[v].back())
      good[v] = 1;
    else
      good[v] = 0;
    ar += good[v];
  } else {
    vst[v]--;
    if (vst[v] == 0) cnt--;
    ar -= good[v];
    if (dir == 1)
      dq[v].pop_back();
    else
      dq[v].pop_front();
    if (((int)dq[v].size()) == 0)
      good[v] = 0;
    else if (nxt[dq[v].front()] >= dq[v].back())
      good[v] = 1;
    else
      good[v] = 0;
    ar += good[v];
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) gn(b[i]);
  for (int i = 0; i < 100011; i++) pos[i] = n + 1;
  for (int i = n; i >= 1; i--) {
    nxt_same[i] = pos[b[i]];
    pos[b[i]] = i;
  }
  for (int i = n; i >= 1; i--) {
    if (nxt_same[i] == n + 1)
      nxt[i] = i;
    else {
      int ni = nxt_same[i];
      int nni = nxt_same[ni];
      if (nni == n + 1)
        nxt[i] = ni;
      else if (ni * 2 == nni + i)
        nxt[i] = nxt[ni];
      else
        nxt[i] = ni;
    }
  }
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    gn(q[i].first.first, q[i].first.second);
    q[i].second = i;
  }
  S = sqrt(n) + 1;
  sort(q, q + m, cmp);
  int st = 1, ed = 0;
  for (int i = 0; i < m; i++) {
    int l = q[i].first.first, r = q[i].first.second, id = q[i].second;
    while (ed < r) poke(++ed, 1, 1);
    while (ed > r) poke(ed--, -1, 1);
    while (st < l) poke(st++, -1, -1);
    while (st > l) poke(--st, 1, -1);
    ans[id] = cnt + (ar == 0);
  }
  for (int i = 0; i < m; i++) println(ans[i]);
  return 0;
}
