#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while(T--) {
int N;
cin >> N;
vector<int> A(N+1);
for(int i = 1; i <= N; i++) {
cin >> A[i];
}
int Q;
cin >> Q;
while(Q--) {
int l, r;
cin >> l >> r;
int k = r - l + 1;
vector<int> freq(k+1, 0);
// Count valid elements in [1..k] for subarray A[l..r]
for(int i = l; i <= r; i++) {
int val = A[i];
if(val >= 1 && val <= k) {
freq[val]++;
}
}
// Count how many distinct values from 1..k
int distinctInRange = 0;
for(int x = 1; x <= k; x++) {
if(freq[x] > 0) {
distinctInRange++;
}
}
// Minimum changes = subarray length - number_of_distinct_valid_elements
cout << (k - distinctInRange) << "\n";
}
}
return 0;
}