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 INF = 1e9;
const int LEAVES = 1 << 17;
int m;
int b[100001];
int q;
vector<pair<int, int> > l[100001];
int t[100001];
int tt[LEAVES * 2];
vector<int> st[100001];
int ans[100001];
void updateSum(int pos, int val) {
  while (pos < 100000) {
    t[pos] += val;
    pos |= pos + 1;
  }
}
int sum(int pos) {
  int res = 0;
  while (pos > -1) {
    res += t[pos];
    pos = (pos & (pos + 1)) - 1;
  }
  return res;
}
int updateMax(int pos, int val) {
  int res = tt[pos += LEAVES];
  tt[pos] = val;
  while (pos >>= 1) tt[pos] = max(tt[pos << 1], tt[pos << 1 | 1]);
  return res;
}
int getMax(int v, int tl, int tr, int l, int r) {
  if (l > r) return -1;
  if (l == tl && r == tr) return tt[v];
  int tm = (tl + tr) >> 1;
  return max(getMax(v << 1, tl, tm, l, min(r, tm)),
             getMax(v << 1 | 1, tm + 1, tr, max(l, tm + 1), r));
}
int main() {
  fill(tt, tt + 2 * LEAVES, -1);
  scanf("%d", &m);
  for (int i = 0; i < m; ++i) scanf("%d", b + i), --b[i];
  scanf("%d", &q);
  for (int i = 0, x, y; i < q; ++i) {
    scanf("%d%d", &x, &y);
    l[--x].push_back(make_pair(--y, i));
  }
  for (int i = m - 1; i > -1; --i) {
    if (st[b[i]].empty()) {
      updateSum(i, 1);
      updateMax(i, INF);
    } else {
      updateSum(st[b[i]].back(), -1);
      updateSum(i, 1);
      int val = updateMax(st[b[i]].back(), -1);
      int jump = i - st[b[i]].back();
      if ((int)st[b[i]].size() == 1 ||
          jump == st[b[i]][(int)st[b[i]].size() - 1] -
                      st[b[i]][(int)st[b[i]].size() - 2]) {
        updateMax(i, val);
      } else {
        updateMax(i, st[b[i]][(int)st[b[i]].size() - 2]);
      }
    }
    st[b[i]].push_back(i);
    for (int j = 0; j < (int)l[i].size(); ++j) {
      int diff = sum(l[i][j].first);
      int mx = getMax(1, 0, LEAVES - 1, i, l[i][j].first);
      ans[l[i][j].second] = diff + (mx <= l[i][j].first);
    }
  }
  for (int i = 0; i < q; ++i) printf("%d\n", ans[i]);
  return 0;
}
