#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define mod 1000000007
 
int main(){
	ios::sync_with_stdio(0);cin.tie(0);
	int t;cin >> t;
	while(t--){
		ll l,r,count;
		cin >> l >> r;
		if(r/2<l)count =0;
		else count = r-l-l+1;
		cout << count*(count+1)/2 << '\n';
		}
}

#include<bits/stdc++.h>
using namespace std;
namespace hyy {
	#define DEBUG 1
	#define isdigit(x) (x >= '0' && x <= '9')
  const int MAXSIZE = (1 << 20);
  inline char gc() {
		#if DEBUG
    return getchar();
		#endif
    static char buf[MAXSIZE];
		static char *p1 = buf + MAXSIZE;
		static char *p2 = buf + MAXSIZE;
    if(p1 == p2) {
      p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
		}
		if(p1 == p2) {
			return -1;
		}
		return *p1++;
  }
	inline bool blank(char ch) {
    return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
  }
  template<class T> inline void read(T &x) {
    register double tmp = 1;
    register bool sign = 0;
    x = 0;
    register char ch = gc();
    for(; !isdigit(ch); ch = gc()) {
      if(ch == '-') {
        sign = 1;
			}
		}
    for(; isdigit(ch); ch = gc()) {
    	x = (x << 1) + (x << 3) + (ch ^ 48);
		}
    if(ch == '.') {
      for(ch = gc(); isdigit(ch); ch = gc()) {
        tmp /= 10.0;
				x += tmp * (ch - 48);
			}
		}
    if(sign) {
      x = -x;
		}
  }
  inline void read(char *s) {
    register char ch = gc();
    for(; blank(ch); ch = gc());
	  for(; !blank(ch); ch = gc()) {
    	*s++=ch;
		}
    *s = 0;
  }
  inline void read(char &c) {
    for(c = gc(); blank(c); c = gc());
  }
  inline void push(const char &c) {
    char pbuf[MAXSIZE];
		char *pp = pbuf;
    if (pp - pbuf == MAXSIZE) {
      fwrite(pbuf, 1, MAXSIZE, stdout);
			pp = pbuf;
		}
  	*pp++=c;
  }
  template<class T> inline void write(T x) {
    static T sta[35];
    T top=0;
    do {
      sta[top++] = x % 10;
			x /= 10;
    }
		while(x);
		#if DEBUG
  	while(top) {
      putchar(sta[--top] + '0');
		}
    return;
		#endif
    while(top) {
      push(sta[--top] + '0');
		}
  }
  template<class T> inline void write(T x, char lastChar) {
    write(x);
		putchar(lastChar);
  }
}
using namespace hyy;
#define MAXN 200005

int lowbit(int x) {
	return x & (-x);
}
int n,sum[MAXN];
void Add(int x,int w) {
	for(;x<=n;sum[x]+=w,x+=lowbit(x));
}
int Sum(int x) {
	int ans=0;
	for(;x;ans+=sum[x],x-=lowbit(x));
	return ans;
}
struct Edge {
	int next,to;
}E[MAXN<<1];
int head[MAXN],cnt;
void AddE(int x,int y) { 
	E[++cnt].next=head[x],E[cnt].to=y,head[x]=cnt;
}
int Q,fa[MAXN],ans[MAXN],dep[MAXN];
struct Qry {
	int y,id;
}p;
vector<Qry>q[MAXN];
void dfs(int x,int fa) {
	dep[x]=dep[fa]+1;
	for(int i=0;i<q[x].size();++i) {
		ans[q[x][i].id]-=Sum(q[x][i].y)-Sum(q[x][i].y-1);
	}
	for(int i=head[x];i;i=E[i].next) {
		dfs(E[i].to,x);
	}
	Add(dep[x],1);
	for(int i=0;i<q[x].size();++i) {
		ans[q[x][i].id]+=Sum(q[x][i].y)-Sum(q[x][i].y-1);
	}
}
int main() {
	scanf("%d",&n);
	for(int i=2;i<=n;++i) {
		scanf("%d",&fa[i]),AddE(fa[i],i);
	}
	scanf("%d",&Q);
	for(int i=0,x,y;i<Q;++i) {
		scanf("%d%d",&x,&y),p.y=y+1,p.id=i,q[x].push_back(p);
	}
	dfs(1,0);
	for(int i=0;i<Q;++i) {
		printf("%d\n",ans[i]);
	}
	return 0;
}