#include <bits/stdc++.h>
using namespace std;
struct query {
  int l, r, p, id;
  bool operator<(const query &b) const {
    return p < b.p || (p == b.p && r < b.r);
  }
} q[100001];
int n, m, a[100001], cnt[100001], d[100001], ans[100001], sum, sumd;
deque<int> v[100001];
unordered_map<int, int> k[100001];
void push(int x, bool front) {
  if (!cnt[a[x]]++) sumd++;
  int p1, p2;
  if (front)
    p1 = 1, p2 = 0, v[a[x]].push_front(x);
  else
    p1 = v[a[x]].size(), p2 = p1 - 1, v[a[x]].push_back(x);
  if (v[a[x]].size() > 1 && !k[a[x]][v[a[x]][p1] - v[a[x]][p2]]++ &&
      k[a[x]].size() == 2)
    sum++;
}
void pop(int x, bool front) {
  if (!--cnt[a[x]]) sumd--;
  int p1, p2;
  if (front)
    p1 = 1, p2 = 0;
  else
    p1 = v[a[x]].size() - 1, p2 = p1 - 1;
  if (v[a[x]].size() > 1 && !--k[a[x]][v[a[x]][p1] - v[a[x]][p2]]) {
    if (k[a[x]].size() == 2) sum--;
    k[a[x]].erase(v[a[x]][p1] - v[a[x]][p2]);
  }
  if (front)
    v[a[x]].pop_front();
  else
    v[a[x]].pop_back();
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", a + i);
  const int z = sqrt(n) + .5;
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &q[i].l, &q[i].r);
    q[i].p = q[i].l / z;
    q[i].id = i;
  }
  sort(q + 1, q + 1 + m);
  int l = 1, r = 0;
  for (int i = 1; i <= m; i++) {
    while (l > q[i].l) push(--l, 1);
    while (r < q[i].r) push(++r, 0);
    while (l < q[i].l) pop(l++, 1);
    while (r > q[i].r) pop(r--, 0);
    ans[q[i].id] = sumd + (sum == sumd);
  }
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
  return 0;
}
