class FenwickTree:
def __init__(self, x):
self.bit = x
for i in range(len(x)):
j = i | (i + 1)
if j < len(x):
x[j] += x[i]
def update(self, idx, x):
while idx < len(self.bit):
self.bit[idx] += x
idx |= idx + 1
def query(self, end):
x = 0
while end:
x += self.bit[end - 1]
end &= end - 1
return x
n = int(input())
arr = list(map(int, input().split()))
adds = [[] for _ in range(n)]
dels = [[] for _ in range(n)]
prev = {}
for i in range(n):
adds[prev.get(arr[i], -1) + 1].append(i)
dels[i].append(i)
prev[arr[i]] = i
q = int(input())
queries = [[] for _ in range(n)]
res = [-1] * q
for i in range(q):
l, r = map(int, input().split())
l -= 1
r -= 1
queries[l].append((r, i))
curr = FenwickTree([0] * (n + 1))
for i in range(n):
for l in adds[i]:
curr.update(l, 1)
for l, pos in queries[i]:
res[pos] = curr.query(l + 1)
for l in dels[i]:
curr.update(l, -1)
print(*res)
Y2xhc3MgRmVud2lja1RyZWU6CiAgICBkZWYgX19pbml0X18oc2VsZiwgeCk6CiAgICAgICAgc2VsZi5iaXQgPSB4CiAgICAgICAgZm9yIGkgaW4gcmFuZ2UobGVuKHgpKToKICAgICAgICAgICAgaiA9IGkgfCAoaSArIDEpCiAgICAgICAgICAgIGlmIGogPCBsZW4oeCk6CiAgICAgICAgICAgICAgICB4W2pdICs9IHhbaV0KCiAgICBkZWYgdXBkYXRlKHNlbGYsIGlkeCwgeCk6CiAgICAgICAgd2hpbGUgaWR4IDwgbGVuKHNlbGYuYml0KToKICAgICAgICAgICAgc2VsZi5iaXRbaWR4XSArPSB4CiAgICAgICAgICAgIGlkeCB8PSBpZHggKyAxCgogICAgZGVmIHF1ZXJ5KHNlbGYsIGVuZCk6CiAgICAgICAgeCA9IDAKICAgICAgICB3aGlsZSBlbmQ6CiAgICAgICAgICAgIHggKz0gc2VsZi5iaXRbZW5kIC0gMV0KICAgICAgICAgICAgZW5kICY9IGVuZCAtIDEKICAgICAgICByZXR1cm4geAogICAgICAgICAgICAKbiA9IGludChpbnB1dCgpKQoKYXJy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