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;
int n, m, L, R;
int cnt[200000 + 5], col[200000 + 5];
int ans[200000 + 5];
int fl[200000 + 5], fr[200000 + 5], pre[200000 + 5], bak[200000 + 5];
int ppos[200000 + 5], pcnt, pn;
inline int read() {
  int f = 1, x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
struct Query {
  int l, r, id, k;
} Q[200000 + 5];
inline bool cmp(Query s1, Query s2) {
  if ((s1.l - 1) / 317 != (s2.l - 1) / 317) return s1.l < s2.l;
  return s1.r < s2.r;
}
inline void Insert(int x, int k) {
  cnt[col[x]]++;
  if (cnt[col[x]] == 1)
    pn++, pcnt++;
  else if (!k && fr[x] <= R && fr[bak[x]] > R)
    pn--;
  else if (k && fl[x] >= L && fl[pre[x]] < L)
    pn--;
}
inline void popout(int x, int k) {
  cnt[col[x]]--;
  if (cnt[col[x]] == 0)
    pn--, pcnt--;
  else if (!k && fr[x] <= R && fr[bak[x]] > R)
    pn++;
  else if (k && fl[x] >= L && fl[pre[x]] < L)
    pn++;
}
int main() {
  n = read();
  for (register int i = 1; i <= n; i++) col[i] = read();
  m = read();
  for (register int i = 1; i <= m; i++) Q[i] = (Query){read(), read(), i};
  for (register int i = 1; i <= n; i++) {
    pre[i] = ppos[col[i]];
    ppos[col[i]] = i;
    if (pre[pre[i]] == 0)
      fl[i] = 0;
    else if (pre[i] - pre[pre[i]] == i - pre[i])
      fl[i] = fl[pre[i]];
    else
      fl[i] = pre[pre[i]];
  }
  memset(ppos, 0, sizeof(ppos));
  for (register int i = n; i >= 1; i--) {
    bak[i] = ppos[col[i]];
    ppos[col[i]] = i;
    if (bak[bak[i]] == 0)
      fr[i] = n + 1;
    else if (bak[i] - bak[bak[i]] == i - bak[i])
      fr[i] = fr[bak[i]];
    else
      fr[i] = bak[bak[i]];
  }
  sort(Q + 1, Q + m + 1, cmp);
  L = 1, R = 0;
  for (register int i = 1; i <= m; i++) {
    while (L < Q[i].l) popout(L++, 0);
    while (L > Q[i].l) Insert(--L, 0);
    while (R > Q[i].r) popout(R--, 1);
    while (R < Q[i].r) Insert(++R, 1);
    ans[Q[i].id] = pcnt + (pn == 0);
  }
  for (register int i = 1; i <= m; i++) printf("%d\n", ans[i]);
}
