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 long long LMIN = numeric_limits<long long>::min();
const long long LMAX = numeric_limits<long long>::max();
const int N = 1e5 + 1;
const int Bsize = 316;
const int Bcount = N / Bsize + 1;
int n, q;
int l, r;
int curb = -1, indb = 0;
int a[N];
pair<int, int> qu[N];
int ans[N];
vector<int> qinb[Bcount];
struct NUM {
  int ind;
  int dr;
  bool good;
  NUM() {}
  NUM(int a, int b, bool c) {
    ind = a;
    dr = b;
    good = c;
  }
};
bool used[N];
deque<int> seq[N];
int fbi[N];
int difc = 0;
int difs = 0;
bool cmp(int a, int b) { return (qu[a].second < qu[b].second); }
void ent_ans() {
  ans[qinb[curb][indb]] = difc;
  if (difs <= 0) {
    ans[qinb[curb][indb]]++;
  }
}
void print_ans() {
  for (int i = 0; i < q; i++) {
    cout << ans[i] << '\n';
  }
}
void plus_r() {
  r++;
  if (used[a[r]] == true) {
    if ((int)seq[a[r]].size() == 1) {
      seq[a[r]].push_back(r);
    } else {
      if (seq[a[r]][(int)seq[a[r]].size() - 1] -
              seq[a[r]][(int)seq[a[r]].size() - 2] ==
          r - seq[a[r]][(int)seq[a[r]].size() - 1]) {
        seq[a[r]].push_back(r);
      } else {
        if (fbi[a[r]] == -1) {
          difs--;
        }
        seq[a[r]].push_back(r);
        fbi[a[r]] = seq[a[r]][(int)seq[a[r]].size() - 3];
      }
    }
  } else {
    used[a[r]] = true;
    difc++;
    difs++;
    seq[a[r]].push_back(r);
  }
}
void minus_l() {
  l--;
  if (used[a[l]] == true) {
    if ((int)seq[a[l]].size() == 1) {
      seq[a[l]].push_front(l);
    } else {
      if (fbi[a[l]] == -1 && seq[a[l]][1] - seq[a[l]][0] != seq[a[l]][0] - l) {
        fbi[a[l]] = l;
        seq[a[l]].push_front(l);
        difs--;
      } else {
        seq[a[l]].push_front(l);
      }
    }
  } else {
    used[a[l]] = true;
    difs++;
    difc++;
    seq[a[l]].push_front(l);
  }
}
void plus_l() {
  if ((int)seq[a[l]].size() == 1) {
    used[a[l]] = false;
    difc--;
    difs--;
    seq[a[l]].pop_front();
  } else {
    if (fbi[a[l]] == -1) {
      seq[a[l]].pop_front();
    } else {
      if (l == fbi[a[l]]) {
        fbi[a[l]] = -1;
        difs++;
        seq[a[l]].pop_front();
      } else {
        seq[a[l]].pop_front();
      }
    }
  }
  l++;
}
void shift_block(int old) {
  for (int i = 0; i < N; i++) {
    used[i] = false;
    seq[i].clear();
    fbi[i] = -1;
  }
  difc = 0;
  difs = 0;
  for (int i = curb + 1; i < Bcount; i++) {
    if (!qinb[i].empty()) {
      curb = i;
      indb = 0;
      break;
    }
  }
  if (old == curb) {
    print_ans();
    exit(0);
  }
  l = qu[qinb[curb][0]].first;
  used[a[l]] = true;
  difc++;
  difs++;
  seq[a[l]].push_back(l);
  r = l;
  while (r < qu[qinb[curb][0]].second) {
    plus_r();
  }
  ent_ans();
  indb++;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> l >> r;
    l--;
    r--;
    qu[i] = make_pair(l, r);
    qinb[l / Bsize].push_back(i);
  }
  for (int i = 0; i < Bcount; i++) {
    sort(qinb[i].begin(), qinb[i].end(), cmp);
  }
  shift_block(curb);
  while (true) {
    if (indb == (int)qinb[curb].size()) {
      shift_block(curb);
    } else {
      while (r < qu[qinb[curb][indb]].second) {
        plus_r();
      }
      while (l < qu[qinb[curb][indb]].first) {
        plus_l();
      }
      while (l > qu[qinb[curb][indb]].first) {
        minus_l();
      }
      ent_ans();
      indb++;
    }
  }
}
