#include <bits/stdc++.h>
#define pb push_back
#define all(x) (x).begin(), (x).end()
#ifdef KAZAR
#define eprintf(...) fprintf(stderr,__VA_ARGS__)
#else
#define eprintf(...) 0
#endif
using namespace std;
template<class T> inline void umax(T &a,T b){if(a < b) a = b;}
template<class T> inline void umin(T &a,T b){if(a > b) a = b;}
template<class T> inline T abs(T a){return a > 0 ? a : -a;}
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
const int inf = 1e9 + 143;
const ll longinf = 1e18 + 143;
inline int read(){int x;scanf(" %d",&x);return x;}
const int N = 1e6 + 100;
int c[N], a[N];
int l[N], r[N], ord[N];
int ans[N];
int main(){
#ifdef KAZAR
freopen("f.input","r",stdin);
freopen("f.output","w",stdout);
freopen("error","w",stderr);
#endif
int n = read();
for (int i = 0; i < n; i++) {
a[i] = read();
}
int len = 1;
while (len * len < n)
++len;
int q = read();
for (int i = 0; i < q; i++) {
l[i] = read() - 1;
r[i] = read() - 1;
ord[i] = i;
}
sort(ord, ord + q, [&](int a,int b){
if (l[a] / len == l[b] / len)
return r[a] < r[b];
return l[a] / len < l[b] / len;
});
int cur = 0;
auto add = [&cur](int id) {
if (c[a[id]] == 0)
++cur;
c[a[id]]++;
};
auto del = [&cur](int id) {
if (c[a[id]] == 1)
--cur;
c[a[id]]--;
};
int L = 0, R = 0;
add(0);
for (int _i = 0; _i < q; _i++) {
int i = ord[_i];
while (R > r[i]) del(R--);
while (R < r[i]) add(++R);
while (L < l[i]) del(L++);
while (L > l[i]) add(--L);
ans[i] = cur;
}
for (int i = 0; i < q; i++) {
printf("%d\n", ans[i]);
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CgojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIGFsbCh4KSAoeCkuYmVnaW4oKSwgKHgpLmVuZCgpCgojaWZkZWYgS0FaQVIKICAjZGVmaW5lIGVwcmludGYoLi4uKSBmcHJpbnRmKHN0ZGVycixfX1ZBX0FSR1NfXykK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