//Pranet Verma
#include <bits/stdc++.h>
using namespace std;
int base,n,k,m,h[100001],b[100001],lt[100001],rt[100001];
long long tot=0;
struct Q
{
int l,r,i;
bool operator <(const Q &o)const{return l/base==o.l/base?r<o.r:l/base<o.l/base;}
}q[100001];
int bit[100011];
int read(int u)
{
int ret=0;
while(u)
ret+=bit[u],u-=(u&-u);
return ret;
}
void update(int i,int u)
{
while(i<100010)
bit[i]+=u,i+=(i&-i);
}
void add(int u)
{
tot+=read(rt[u])-read(lt[u]-1);
update(h[u],1);
}
void remove(int u)
{
update(h[u],-1);
tot-=read(rt[u])-read(lt[u]-1);
}
int L=0,R=-1;
long long process(int l,int r)
{
while(L>l)
add(--L);
while(L<l)
remove(L++);
while(R>r)
remove(R--);
while(R<r)
add(++R);
return tot;
}
long long ret[100001];
int main()
{
scanf("%d%d",&n,&k);
base=ceil(sqrt(n));
for(int i=0;i<n;++i)
scanf("%d",&h[i]),
b[i]=h[i];
sort(b,b+n);
for(int i=0;i<n;++i)
lt[i]=1+lower_bound(b,b+n,h[i]-k)-b,
rt[i]=1+upper_bound(b,b+n,h[i]+k)-b-1,
h[i]=1+lower_bound(b,b+n,h[i])-b;
scanf("%d",&m);
for(int i=0;i<m;++i)
scanf("%d%d",&q[i].l,&q[i].r),q[i].i=i;
sort(q,q+m);
for(int i=0;i<m;++i)
ret[q[i].i]=process(q[i].l,q[i].r);
for(int i=0;i<m;++i)
printf("%lld\n",ret[i]);
}
Ly9QcmFuZXQgVmVybWEKI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmludCBiYXNlLG4sayxtLGhbMTAwMDAxXSxiWzEwMDAwMV0sbHRbMTAwMDAxXSxydFsxMDAwMDFdOwpsb25nIGxvbmcgdG90PTA7CnN0cnVjdCBRCnsKICAgIGludCBsLHIsaTsKICAgIGJvb2wgb3BlcmF0b3IgPChjb25zdCBRICZvKWNvbnN0e3JldHVybiBsL2Jhc2U9PW8ubC9iY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gcnRbaV09MSt1cHBlcl9ib3VuZChiLGIrbixoW2ldK2spLWItMSwKICAgICAgICBoW2ldPTErbG93ZXJfYm91bmQoYixiK24saFtpXSktYjsKICAgIHNjYW5mKCIlZCIsJm0pOwogICAgZm9yKGludCBpPTA7aTxtOysraSkKICAgICAgICBzY2FuZigiJWQlZCIsJnFbaV0ubCwmcVtpXS5yKSxxW2ldLmk9aTsKICAgIHNvcnQocSxxK20pOwogICAgZm9yKGludCBpPTA7aTxtOysraSkKICAgICAgICByZXRbcVtpXS5pXT1wcm9jZXNzKHFbaV0ubCxxW2ldLnIpOwogICAgZm9yKGludCBpPTA7aTxtOysraSkKICAgICAgICBwcmludGYoIiVsbGRcbiIscmV0W2ldKTsKfQ==