#include <bits/stdc++.h>
using namespace std;
int II() {
  int q;
  scanf("%d", &q);
  return q;
}
long long LII() {
  long long q;
  scanf("%lld", &q);
  return q;
}
const int Mx = 1000005;
const int INF = 1e9 + 7;
int N;
int ar[Mx], L[Mx], is[Mx], pre[Mx];
struct info {
  int mx, cnt;
};
info tree[Mx * 4];
info combine(info l, info r) { return {min(l.mx, r.mx), l.cnt + r.cnt}; }
void build(int node, int b, int e) {
  tree[node] = {INF, 0};
  if (b == e) {
    tree[node] = {INF, 0};
  } else {
    int mid = (b + e) / 2;
    build(node * 2, b, mid);
    build(node * 2 + 1, mid + 1, e);
  }
}
info query(int node, int b, int e, int l, int r) {
  if (l > e or r < b) return {INF, 0};
  if (b >= l and e <= r) return tree[node];
  int mid = (b + e) / 2;
  return combine(query(node * 2, b, mid, l, r),
                 query(node * 2 + 1, mid + 1, e, l, r));
}
void update(int node, int b, int e, int in, info P) {
  if (in < b or in > e) return;
  if (b == e && in == e) {
    tree[node] = P;
  } else {
    int mid = (b + e) / 2;
    update(node * 2, b, mid, in, P);
    update(node * 2 + 1, mid + 1, e, in, P);
    tree[node] = combine(tree[node * 2], tree[node * 2 + 1]);
  }
}
void PreCalc() {
  fill(L, L + Mx, -INF);
  for (int i = 1; i <= N; i++) {
    pre[i] = L[ar[i]], L[ar[i]] = i;
  }
  for (int i = 1; i <= N; i++) {
    if (pre[i] == -INF || pre[pre[i]] == -INF)
      L[i] = -INF;
    else if (i - pre[i] == pre[i] - pre[pre[i]])
      L[i] = L[pre[i]];
    else
      L[i] = pre[pre[i]];
  }
}
vector<pair<int, int> > v[Mx];
void solve() {
  N = II();
  for (int i = 1; i <= N; i++) ar[i] = II();
  PreCalc();
  build(1, 1, N);
  int q;
  q = II();
  for (int i = 1; i <= q; i++) {
    int l, r;
    l = II(), r = II();
    v[r].push_back({l, i});
  }
  vector<pair<int, int> > ans;
  for (int i = 1; i <= N; i++) {
    if (pre[i] == -INF)
      update(1, 1, N, i, {L[i], 1});
    else
      update(1, 1, N, pre[i], {INF, 0}), update(1, 1, N, i, {L[i], 1});
    for (auto it : v[i]) {
      info Q = query(1, 1, N, it.first, i);
      if (Q.mx >= it.first) Q.cnt++;
      ans.push_back({it.second, Q.cnt});
    }
  }
  sort(ans.begin(), ans.end());
  for (auto it : ans) {
    printf("%d\n", it.second);
  }
}
int32_t main() {
  int tt;
  tt = 1;
  int cas = 1;
  while (tt--) {
    solve();
  }
}
