#include<bits/stdc++.h>
#define endl "\n"
#include "infection.h"
using namespace std;
struct node{
    vector<int> people;
    bool infected;
};
struct person{
    int st,en,day,iday;
    vector<int> path;
    bool infected;
};
int n,m;
vector<int> g[5010];
person a[5010];
node b[5010];

int intime[5010];
int outime[5010];
int Ti;
int p[5010][14];
void dfs(int u,int par){
    intime[u] = Ti++;
    for(auto v: g[u]){
        if(v==par) continue;
        p[v][0] = u;
        dfs(v,u);
    }
    outime[u] = Ti++;
}
void pre(){
    for(int i=1;i<14;i++){
        for(int u=0;u<n;u++){
            p[u][i] = p[p[u][i-1]][i-1];
        }
    }
}
bool isParent(int u,int v){
    return (intime[u]<=intime[v] && outime[v]<=outime[u]);
}
int LCA(int u,int v){
    if(intime[u]>intime[v]) swap(u,v);
    if(isParent(u,v)) return u;
    for(int i=13;i>=0;i--){
        if(!isParent(p[v][i],u)) v = p[v][i];
    }
    return p[v][0];
}
vector<int> getPath(int u,int v){
    vector<int> rez;
    //cout<<"gaf";
    if(intime[u]>intime[v]) swap(u,v);
    int lca = LCA(u,v);
    if(lca==u){
        while(isParent(u,v)){
            rez.push_back(v);
            v = p[v][0];
        }
        reverse(rez.begin(),rez.end());
        return rez;
    }
    //cout<<"gaf";
    while(isParent(lca,u)){
        rez.push_back(u);
        u = p[u][0];
    }
    //cout<<"gaf";
    int left = rez.size();
    while(isParent(lca,v)){
        rez.push_back(v);
        v = p[v][0];
    }
    //cout<<"gaf";
    rez.pop_back();
    reverse(rez.begin()+left+1,rez.end());
    return rez;
}
vector<int> solve(vector<pair<int, int>> R,vector<long long> D,vector<int> S,vector<int> T,vector<bool> I){
    //cout<<"gaf";
    n = R.size()+1;
    m = D.size();
    for(auto p: R){
        g[p.first+1].push_back(p.second+1);
        g[p.second+1].push_back(p.first+1);
    //cout<<"gaf";
    }
    //cout<<"gaf";
    dfs(1,0);
    //cout<<"gaf";
    outime[0] = Ti;
    pre();
    //cout<<"gaf";
    for(int i=0;i<m;i++){
        a[i] = {S[i]+1,T[i]+1,D[i],(I[i] ? D[i] : -1),getPath(S[i]+1,T[i]+1),I[i]};
        for(auto x: a[i].path){
            //cout<<x<<" ";
        }
        //cout<<endl;
    }
    for(int t=0;t<n;t++){
        for(int i=0;i<m;i++){
            if(t<a[i].day || t-a[i].path.size()>=a[i].day) continue;
            int idx = t-a[i].day;
            b[a[i].path[idx]].people.push_back(i);
            if(a[i].infected) b[a[i].path[idx]].infected = true;
        }
        for(int i=1;i<=n;i++){
                //cout<<i<<"   ";
                for(auto u: b[i].people){
                    //cout<<u<<" ";

                }
        //cout<<endl;
            if(b[i].infected){
                for(auto idx: b[i].people){
                    a[idx].infected = true;
                    a[idx].iday = t;
                }
            }
            b[i].infected = false;
            b[i].people.clear();
        }
        //cout<<endl;
    }
    vector<int> ans(m);
    for(int i=0;i<m;i++){
        ans[i] = (a[i].infected ? 0 : a[i].day+a[i].path.size()-a[i].iday);
    }
    return ans;
}/*
int main(){
    int nn,mm;
    cin>>nn>>mm;
    vector<pair<int,int> > r(nn-1);
    vector<long long> d(mm);
    vector<int> s(mm);
    vector<int> t(mm);
    vector<bool> ii(mm);
    for(int i=0;i<nn-1;i++){
        cin>>r[i].first>>r[i].second;
    }
    for(int i=0;i<mm;i++){
            int x;
        cin>>d[i]>>s[i]>>t[i]>>x;
    ii[i] = (bool)(x);
    }
    vector<int> ans = solve(r,d,s,t,ii);
    for(auto x: ans){
        //cout<<x<<" ";
    }
}*/
