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 MX = 100005;
int x[MX];
bool check[MX];
int sum[MX];
long long ans[MX];
const int BLOCK = sqrt(MX);
int n, q;
vector<deque<int> > dq(100005);
vector<deque<int> > dqd(100005);
vector<deque<int> > dqf(100005);
struct node {
  int L, R, INDEX;
} Q[MX];
bool cmp(node x, node y) {
  if (x.L / BLOCK != y.L / BLOCK) {
    return x.L / BLOCK < y.L / BLOCK;
  } else {
    return x.R < y.R;
  }
}
bool counter(int X) {
  int SZ = dq[X].size();
  if (SZ == 0)
    return false;
  else if (SZ == 1 || SZ == 2)
    return true;
  else {
    if (sum[X] == 0)
      return true;
    else
      return false;
  }
}
void MOS() {
  int LEFT, RIGHT, START, END, TEMP;
  START = END = Q[0].L;
  dq[x[START]].push_back(START);
  check[x[START]] = true;
  int unq = 1, cnt = 1;
  for (int i = 0; i < q; i++) {
    while (START < Q[i].L) {
      dq[x[START]].pop_front();
      if (!dqd[x[START]].empty()) dqd[x[START]].pop_front();
      if (!dqf[x[START]].empty()) {
        sum[x[START]] -= dqf[x[START]].front();
        dqf[x[START]].pop_front();
      }
      if (dq[x[START]].empty()) unq--;
      if (counter(x[START]) == false && check[x[START]] == true) {
        cnt--;
        check[x[START]] = false;
      } else if (counter(x[START]) == true && check[x[START]] == false) {
        cnt++;
        check[x[START]] = true;
      }
      START++;
      if (START > END) {
        if (!dq[x[START]].empty()) TEMP = START - dq[x[START]].back();
        if (dqd[x[START]].size() > 0) {
          if (dqd[x[START]].back() == TEMP)
            dqf[x[START]].push_back(0);
          else {
            dqf[x[START]].push_back(1);
            sum[x[START]] += 1;
          }
        }
        if (dq[x[START]].size() > 0) dqd[x[START]].push_back(TEMP);
        dq[x[START]].push_back(START);
        if (dq[x[START]].size() == 1) unq++;
        END = START;
        if (counter(x[START]) == false && check[x[START]] == true) {
          cnt--;
          check[x[START]] = false;
        } else if (counter(x[START]) == true && check[x[START]] == false) {
          cnt++;
          check[x[START]] = true;
        }
      }
    }
    while (START > Q[i].L) {
      START--;
      if (dq[!x[START]].empty()) TEMP = dq[x[START]].front() - START;
      if (dqd[x[START]].size() > 0) {
        if (dqd[x[START]].front() == TEMP)
          dqf[x[START]].push_front(0);
        else {
          dqf[x[START]].push_front(1);
          sum[x[START]] += 1;
        }
      }
      if (dq[x[START]].size() > 0) dqd[x[START]].push_front(TEMP);
      dq[x[START]].push_front(START);
      if (dq[x[START]].size() == 1) unq++;
      if (counter(x[START]) == false && check[x[START]] == true) {
        cnt--;
        check[x[START]] = false;
      } else if (counter(x[START]) == true && check[x[START]] == false) {
        cnt++;
        check[x[START]] = true;
      }
    }
    while (END < Q[i].R) {
      END++;
      if (!dq[x[END]].empty()) TEMP = END - dq[x[END]].back();
      if (dqd[x[END]].size() > 0) {
        if (dqd[x[END]].back() == TEMP)
          dqf[x[END]].push_back(0);
        else {
          dqf[x[END]].push_back(1);
          sum[x[END]] += 1;
        }
      }
      if (dq[x[END]].size() > 0) dqd[x[END]].push_back(TEMP);
      dq[x[END]].push_back(END);
      if (dq[x[END]].size() == 1) unq++;
      if (counter(x[END]) == false && check[x[END]] == true) {
        cnt--;
        check[x[END]] = false;
      } else if (counter(x[END]) == true && check[x[END]] == false) {
        cnt++;
        check[x[END]] = true;
      }
    }
    while (END > Q[i].R) {
      dq[x[END]].pop_back();
      if (!dqd[x[END]].empty()) dqd[x[END]].pop_back();
      if (!dqf[x[END]].empty()) {
        sum[x[END]] -= dqf[x[END]].back();
        dqf[x[END]].pop_back();
      }
      if (dq[x[END]].empty()) unq--;
      if (counter(x[END]) == false && check[x[END]] == true) {
        cnt--;
        check[x[END]] = false;
      } else if (counter(x[END]) == true && check[x[END]] == false) {
        cnt++;
        check[x[END]] = true;
      }
      END--;
    }
    if (cnt > 0)
      ans[Q[i].INDEX] = unq;
    else
      ans[Q[i].INDEX] = unq + 1;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &x[i]);
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &Q[i].L, &Q[i].R);
    Q[i].INDEX = i;
  }
  sort(Q, Q + q, cmp);
  MOS();
  for (int i = 0; i < q; i++) cout << ans[i] << endl;
}
