You are given a tree consisting of n vertices. Recall that a tree is an undirected connected acyclic graph. The given tree is rooted at the vertex 1.

You have to process q queries. In each query, you are given a vertex of the tree v and an integer k.

To process a query, you may delete any vertices from the tree in any order, except for the root and the vertex v. When a vertex is deleted, its children become the children of its parent. You have to process a query in such a way that maximizes the value of c(v) - m ⋅ k (where c(v) is the resulting number of children of the vertex v, and m is the number of vertices you have deleted). Print the maximum possible value you can obtain.

The queries are independent: the changes you make to the tree while processing a query don't affect the tree in other queries.

Input

The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of vertices in the tree.

Then n-1 lines follow, the i-th of them contains two integers x_i and y_i (1 ≤ x_i, y_i ≤ n; x_i ≠ y_i) — the endpoints of the i-th edge. These edges form a tree.

The next line contains one integer q (1 ≤ q ≤ 2 ⋅ 10^5) — the number of queries.

Then q lines follow, the j-th of them contains two integers v_j and k_j (1 ≤ v_j ≤ n; 0 ≤ k_j ≤ 2 ⋅ 10^5) — the parameters of the j-th query.

Output

For each query, print one integer — the maximum value of c(v) - m ⋅ k you can achieve.

Example

Input


8
6 7
3 2
8 3
5 7
7 4
7 1
7 3
6
1 0
1 2
1 3
7 1
5 0
7 200000


Output


5
2
1
4
0
4

Note

The tree in the first example is shown in the following picture:

<image>

Answers to the queries are obtained as follows:

  1. v=1,k=0: you can delete vertices 7 and 3, so the vertex 1 has 5 children (vertices 2, 4, 5, 6, and 8), and the score is 5 - 2 ⋅ 0 = 5; 
  2. v=1,k=2: you can delete the vertex 7, so the vertex 1 has 4 children (vertices 3, 4, 5, and 6), and the score is 4 - 1 ⋅ 2 = 2. 
  3. v=1,k=3: you shouldn't delete any vertices, so the vertex 1 has only one child (vertex 7), and the score is 1 - 0 ⋅ 3 = 1; 
  4. v=7,k=1: you can delete the vertex 3, so the vertex 7 has 5 children (vertices 2, 4, 5, 6, and 8), and the score is 5 - 1 ⋅ 1 = 4; 
  5. v=5,k=0: no matter what you do, the vertex 5 will have no children, so the score is 0; 
  6. v=7,k=200000: you shouldn't delete any vertices, so the vertex 7 has 4 children (vertices 3, 4, 5, and 6), and the score is 4 - 0 ⋅ 200000 = 4. 
#define ssh(x)<x>
#include ssh(bits/stdc++.h)
#define rfor(x...)for(register x)
#define rep(i,l,r)rfor(ll i=l;i<=r;i++)
#define ref(i,l,r)rfor(ll i=l;i<r;i++)
#define per(i,l,r)rfor(ll i=l;i>=r;i--)
#define rev(i,u,v)rfor(ll i=head[u],v;v=to[i],i;i=next[i])
#define rej(i,s)rfor(ll i=s;i;i=(i-1)&(s))
#define red(i,n)rfor(ll i=n;i;i>>=1)
#define IO(x...)*#x&&freopen(#x".in","r",stdin)&&freopen(#x".out","w",stdout),gc=fgc,pc=fpc,atexit(fout)
typedef long long ll;
typedef unsigned long long ull;
const ll N=1e6+7,mod=1e9+7,iz=1<<21,oz=1<<22;
typedef ll aN[N];
char is[iz],*i1=is,*i2=is,os[oz],*o1=os,*o2=os+oz;
int fgc(){
    return i1==i2&&is==(i2=is+fread(i1=is,1,iz,stdin))?-1:*i1++;
}
void fout(){
    fwrite(os,o1-os,1,stdout),o1=os;
}
int fpc(int c){
    if(o1==o2)fout();
    return*o1++=c;
}
int(*gc)()=getchar,(*pc)(int)=putchar,wt[126];
int readchar(int l=33,int r=126){
    if(l>r)std::swap(l,r);
    int c=gc();
    for(;c<l||c>r;c=gc());
    return c;
}
ll readll(){
    ll x=0,w=1;
    int c=gc();
    for(;c<48||c>57;c=gc())c-45||(w=-w);
    for(;c>47&&c<58;c=gc())x=x*10+c-48;
    return x*w;
}
void writell(ll u,int c=32){
    int t=0;
    rfor(ull n=u<0?pc(45),0ull-u:u;n;n/=10)wt[++t]=n%10^48;
    for(t||pc(48);t;)pc(wt[t--]);
    c&&pc(c);
}
int mygets(char*s,int c=0){
    char*t=s+1;
    while((*s=gc())<33);
    while((*t=gc())>32)t++;
    c&&(*t++=c),*t=0;
    return t-s;
}
int myputs(const char*s,int c=10){
    const char*t=s;
    while(*t)pc(*t++);
    c&&pc(c);
    return t-s+(c!=0);
}
aN f,next,to,head,lf,rt,ans,ft;
struct ln{
    ll k,b;
    ll y(ll x)const{
        return k*x+b;
    }
    operator ll()const{
        return b/-k;
    }
    ln&operator+=(const ln&m){
        k+=m.k,b+=m.b;
        return*this;
    }
    ln operator-()const{
        return(ln){-k,-b};
    }
    ln&operator-=(const ln&m){
        k-=m.k,b-=m.b;
        return*this;
    }
    void print(int c=0)const{
        printf("y=%lldx%+lld\n",k,b);
        c&&putchar(c);
    }
}a[N];
struct node{
    ll u,v;
    operator ll()const{
        return v;
    }
};
std::priority_queue<node>qu;
ll cnt,top;
struct bit{
    ln f[N];
    ll n;
    void set(ll n_){
        n=n_;
    }
    void update(ll u,const ln&c){
        for(;u<=n;u+=u&-u)f[u]+=c;
    }
    ln query(ll u){
        ln res={0,0};
        for(;u;u&=~-u)res+=f[u];
        return res;
    }
    ln query(ll l,ll r){
        ln res=query(r);
        res-=query(l-1);
        //myputs("res:",0),res.print();
        return res;
    }
}t;
struct ss{
    ll id,u,k;
    void read(ll i){
        id=i,u=readll(),k=readll();
    }
    operator ll()const{
        return-k;
    }
}s[N];
ll find(ll u){
    return f[u]^u?f[u]=find(f[u]):u;
}
void add(ll u,ll v){
    next[++cnt]=head[u],to[head[u]=cnt]=v;
}
void dfs(ll u,ll fa){
    ft[u]=fa,lf[u]=++top,a[u].k=a[u].b=-1;
    rev(i,u,v)if(v^fa)dfs(v,u),a[u].b++;
    if(a[u].b!=-1)qu.push((node){u,(ll)a[u]});
    t.update(lf[u],a[u]);
    if(u!=1)t.update(lf[ft[u]],-a[u]);
    rt[u]=top;
}
int main(){
    IO();
    ll n=readll(),u,v;
    t.set(n);
    rep(i,1,n)f[i]=i;
    rep(i,2,n)u=readll(),v=readll(),add(u,v),add(v,u);
    dfs(1,0);
    ll q=readll();
    rep(i,1,q)s[i].read(i);
    //rep(i,1,n)a[i].print();
    std::sort(s+1,s+q+1);
    ll p=1;
    per(i,n,0){
        while(!qu.empty()&&i<qu.top().v){
            ll u=qu.top().u,g=qu.top().v;
            qu.pop();
            //puts("???");
            if(f[u]!=u||(ll)a[u]!=g)continue;
            while(u!=1&&i<(ll)a[u]){
                ll v=find(ft[u]);
                //printf("%lld %lld %lld\n",u,v,i);
                a[f[u]=v]+=a[u],
                t.update(lf[ft[u]],a[u]);
                //a[v].print();
                if(ft[v])t.update(lf[ft[v]],-a[u]);
                //printf("%lld %lld\n",u,v);
                u=v;
            }
            if(u!=1)qu.push((node){u,(ll)a[u]});
        //exit(0);
        }
        while(p<=q&&s[p].k>=i)ans[s[p].id]=t.query(lf[s[p].u],rt[s[p].u]).y(s[p].k)+s[p].k+1,p++;
    }
    rep(i,1,q)writell(ans[i],10);
    return 0;
}
