#include <bits/stdc++.h>
#define all(v) begin(v), end(v)
#define ii pair<int, int>
#define fi first
#define se second
#define siz(v) (int)(v).size()
#define lli pair<long long, int>
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(i) (1LL << (i))
using namespace std;
bool M1;
const int MAXN = 4e5 + 5, LOG = 7, MAXV = 1e6 + 6, infINT = 1e9 + 12312;
template<class X, class Y> bool minimize(X &x, const Y &y){return x > y ? x = y, 1: 0;}
template<class X, class Y> bool maximize(X &x, const Y &y){return x < y ? x = y, 1: 0;}
struct M{
int sta, fin;
M(int _sta = 0, int _fin = 0):
sta(_sta), fin(_fin) {};
bool operator <(const M &other) const{
if (fin != other.fin) return fin < other.fin;
return sta > other.sta;
}
};
int numVal, numQuery, best[MAXN], up[LOG][MAXN], par[MAXN];
long long pre[MAXN];
vector<long long > comp;
vector<M > events;
void input(){
cin >> numVal;
comp.push_back(0);
for(int i = 1; i <= numVal; i++){
cin >> pre[i];
pre[i] += pre[i - 1];
comp.push_back(pre[i]);
}
}
int getPos(const long long &pos){
return lower_bound(all(comp), pos) - begin(comp) + 1;
}
#define minID(a, b) (events[a].fin < events[b].fin ? a: b)
void solve(){
sort(all(comp));
comp.resize(unique(all(comp)) - begin(comp));
for(int i = 0; i <= numVal; i++)
pre[i] = getPos(pre[i]);
memset(best, -1, sizeof best);
for(int i = 0; i <= numVal; i++){
if (best[pre[i]] != -1) events.push_back(M(best[pre[i]] + 1, i));
best[pre[i]] = i;
}
sort(all(events));
events.push_back(M(numVal + 1, numVal + 1));
for(int i = 0; i <= numVal + 2; i++) best[i] = siz(events) - 1;
for(int i = 0; i < siz(events); i++)
best[events[i].sta] = minID(best[events[i].sta], i);
for(int i = numVal; i >= 1; i--)
best[i] = minID(best[i], best[i + 1]);
for(int i = 0; i < siz(events); i++){
par[i] = up[0][i] = best[events[i].fin + 1];
}
for(int j = 1; j < LOG; j++){
for(int u = 0; u < siz(events); u++){
up[j][u] = up[j - 1][up[j - 1][u]];
}
}
for(int u = 0; u < siz(events); u++) up[0][u] = up[LOG - 1][up[LOG - 1][u]];
for(int j = 1; j < LOG; j++){
for(int u = 0; u < siz(events); u++){
up[j][u] = up[j - 1][up[j - 1][u]];
}
}
cin >> numQuery;
for(int q = 1; q <= numQuery; q++){
int sta, fin; cin >> sta >> fin;
int u = best[sta], res = 0;
for(int i = LOG - 1; i >= 0; i--) while (events[up[i][u]].fin <= fin) {
u = up[i][u];
res += MASK(i) * 128;
}
int cnt = 0;
while(events[par[u]].fin <= fin){
u = par[u], res++; cnt++;
}
if (events[u].fin <= fin) res++;
cout << res << '\n';
}
}
bool M2;
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "test"
if (fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
input();
solve();
cerr << (1.0 * clock()) / CLOCKS_PER_SEC << ".s\n";
cerr << (&M2 - &M1) / 1048576 << " mb\n";
}