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;
inline char gc() {
  static char buf[1 << 20], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline int read() {
  int ret = 0, f = 1;
  char ch = gc();
  while ((ch < '0' || ch > '9') && ch != EOF) {
    if (ch == '-') f = -1;
    ch = gc();
  }
  while (ch >= '0' && ch <= '9') {
    ret = ret * 10 + ch - '0';
    ch = gc();
  }
  return ret * f;
}
const int INF = (1 << 30) - 1;
const int NR = 1e5 + 10, MR = 1e5 + 10, CR = 1e5 + 10;
int n, m, a[NR], ans[MR];
int mnv[NR << 2], treearr[NR];
int lp[NR], dis[NR], lt[NR];
struct Q {
  int id, l, r;
} q[MR];
int lowbit(int x) { return x & (-x); }
void bitmodify(int p, int dt) {
  for (; p <= n; p += lowbit(p)) treearr[p] += dt;
}
int bitcalc(int p) {
  int ret = 0;
  for (; p; p -= lowbit(p)) ret += treearr[p];
  return ret;
}
void update(int p) { mnv[p] = min(mnv[(p << 1)], mnv[(p << 1 | 1)]); }
void modify(int p, int l, int r, int pt, int dt) {
  if (l == r) return (void)(mnv[p] = dt);
  if (pt <= ((l + r) >> 1))
    modify((p << 1), l, ((l + r) >> 1), pt, dt);
  else
    modify((p << 1 | 1), ((l + r) >> 1) + 1, r, pt, dt);
  update(p);
}
int calc(int p, int l, int r, int pl, int pr) {
  if (pl <= l && pr >= r) return mnv[p];
  int ret = INF;
  if (pl <= ((l + r) >> 1))
    ret = min(ret, calc((p << 1), l, ((l + r) >> 1), pl, pr));
  if (pr > ((l + r) >> 1))
    ret = min(ret, calc((p << 1 | 1), ((l + r) >> 1) + 1, r, pl, pr));
  return ret;
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  m = read();
  for (int i = 1; i <= m; i++) q[i].l = read(), q[i].r = read(), q[i].id = i;
  sort(q + 1, q + m + 1, [](Q x, Q y) { return x.r < y.r; });
  int cur = 1;
  for (int i = 1; i <= n; i++) {
    if (!lp[a[i]])
      modify(1, 1, n, i, 1);
    else {
      bitmodify(lp[a[i]], -1);
      modify(1, 1, n, lp[a[i]], INF);
      if (!dis[a[i]])
        modify(1, 1, n, i, 1);
      else if (i - lp[a[i]] == dis[a[i]]) {
        lt[i] = lt[lp[a[i]]];
        modify(1, 1, n, i, lt[i]);
      } else {
        lt[i] = lp[a[i]] - dis[a[i]] + 1;
        modify(1, 1, n, i, lt[i]);
      }
      dis[a[i]] = i - lp[a[i]];
    }
    lp[a[i]] = i;
    bitmodify(i, 1);
    while (q[cur].r == i && cur <= m) {
      ans[q[cur].id] = bitcalc(i) - bitcalc(q[cur].l - 1);
      if (calc(1, 1, n, q[cur].l, i) > q[cur].l) ans[q[cur].id]++;
      cur++;
    }
  }
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
  return 0;
}
