#include <bits/stdc++.h>
using namespace std;
const int SQ = 300;
struct Query {
  int l, r, id;
  bool operator<(const Query& rhs) const {
    return l / SQ < rhs.l / SQ || (l / SQ == rhs.l / SQ && r < rhs.r);
  }
} qs[100005];
int ans[100005];
int v[100005];
int fails[100005];
deque<int> dq[100005];
bool ok(int y, int i) { return dq[y][i] * 2 == dq[y][i + 1] + dq[y][i - 1]; }
int main() {
  int n = ({
    int x;
    scanf("%d", &x);
    x;
  });
  for (int i = 0; i < n; i++) {
    v[i] = ({
      int x;
      scanf("%d", &x);
      x;
    });
  }
  int q = ({
    int x;
    scanf("%d", &x);
    x;
  });
  for (int i = 0; i < q; i++) {
    qs[i] = {({
               int x;
               scanf("%d", &x);
               x;
             }) - 1,
             ({
               int x;
               scanf("%d", &x);
               x;
             }) - 1,
             i};
  }
  sort(qs, qs + q);
  int diff = 0;
  int mov = 0;
  auto add = [&](int x) {
    int y = v[x];
    if (dq[y].empty()) diff++, mov++;
    if (!dq[y].empty() && dq[y][0] > x) {
      dq[y].push_front(x);
      if (dq[y].size() > 2 && !ok(y, 1)) {
        if (!fails[y]) mov--;
        fails[y]++;
      }
    } else {
      dq[y].push_back(x);
      if (dq[y].size() > 2 && !ok(y, dq[y].size() - 2)) {
        if (!fails[y]) mov--;
        fails[y]++;
      }
    }
  };
  auto remove = [&](int x) {
    int y = v[x];
    if (dq[y].size() == 1) mov--, diff--;
    if (dq[y][0] == x) {
      if (dq[y].size() > 2 && !ok(y, 1)) {
        fails[y]--;
        if (!fails[y]) mov++;
      }
      dq[y].pop_front();
    } else {
      if (dq[y].size() > 2 && !ok(y, dq[y].size() - 2)) {
        fails[y]--;
        if (!fails[y]) mov++;
      }
      dq[y].pop_back();
    }
  };
  int L = 0, R = -1;
  for (int k = 0; k < q; k++) {
    while (R < qs[k].r) add(++R);
    while (R > qs[k].r) remove(R--);
    while (L < qs[k].l) remove(L++);
    while (L > qs[k].l) add(--L);
    ans[qs[k].id] = diff + (mov == 0);
  }
  for (int i = 0; i < q; i++) {
    printf("%d\n", ans[i]);
  }
}
