#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll infll = 1e18;
template<typename T>
istream &operator>>(istream &is, vector<T> &a) {
for (auto &i : a) {
is >> i;
}
return is;
}
int32_t main() {
#ifdef LOCAL
freopen("/tmp/input.txt", "r", stdin);
#else
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
int n;
cin >> n;
vector<int> a(n);
cin >> a;
vector<ll> pref(n + 1);
for (int i = 1; i <= n; ++i) {
pref[i] = pref[i - 1] + a[i - 1];
}
vector<int> R(n);
{
map<ll, int> last;
last[pref[n]] = n;
for (int i = n - 1; i >= 0; --i) {
if (last.find(pref[i]) != last.end()) {
R[i] = last[pref[i]] - 1;
} else {
R[i] = n;
}
last[pref[i]] = i;
}
}
vector<int> suf(n + 1, n);
for (int i = n - 1; i >= 0; --i) {
suf[i] = min(suf[i + 1], R[i]);
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
--l, --r;
int cnt = 0;
while (true) {
if (suf[l] <= r) {
l = suf[l] + 1;
++cnt;
} else {
break;
}
}
cout << cnt << "\n";
}
return 0;
}