#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> v;
typedef vector<v> vv;
typedef vector<vv> vvv;
typedef pair<ll, ll> p;
typedef vector<p> vp;
typedef vector<vp> vvp;
typedef vector<vvp> vvvp;
typedef pair<ll, p> tri;
typedef vector<tri> vtri;
typedef vector<vtri> vvtri;
typedef vector<vvtri> vvvtri;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;

#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()

const ll INF = 1e18;
const ll mod = 1e9 + 7;

void initStuff(ll i, vv &graph, v &height, v &parent)
{
    for (ll x : graph[i])
    {
        if (height[x] == -1)
        {
            height[x] = height[i] + 1;
            parent[x] = i;
            initStuff(x, graph, height, parent);
        }
    }
}

std::vector<int> solve(
std::vector<std::pair<int, int>> R,
std::vector<long long> D,
std::vector<int> S,
std::vector<int> T,
std::vector<bool> I
)
{
    ll N = R.size() + 1;
    vv graph(N);
    for (ll i = 0; i < N - 1; i++)
    {
        graph[R[i].f].pb(R[i].s);
        graph[R[i].s].pb(R[i].f);
    }
    v height(N, -1);
    height[0] = 0;
    v parent(N, -1);
    initStuff(0, graph, height, parent);
    ll M = D.size();
    vv paths;
    unordered_map<ll, unordered_map<ll, v>> people;
    v infectionDay(M, INF);
    v pathSize(M);
    for (ll i = 0; i < M; i++)
    {
        ll a = S[i], b = T[i];
        if (I[i]) infectionDay[i] = D[i];
        v pathA, pathB;
        ll rev = 0;
        while (a != b)
        {
            if (height[a] < height[b])
            {
                swap(a, b);
                swap(pathA, pathB);
                rev = 1 - rev;
            }
            pathA.pb(a);
            a = parent[a];
        }
        pathA.pb(a);
        if (rev)
            swap(pathA, pathB);
        reverse(all(pathB));
        for (ll x : pathB) pathA.pb(x);
        for (ll j = 0; j < pathA.size(); j++) people[D[i] + j][pathA[j]].pb(i);
        pathSize[i] = pathA.size();
    }
    for (auto &x : people)
    {
        ll day = x.f;
        for (auto &y : x.s)
        {
            bool hasInfected = false;
            for (auto &z : y.s)
            {
                if (I[z]) hasInfected = true;
            }
            if (hasInfected)
            {
                for (auto &z : y.s)
                {
                    if (!I[z])
                    {
                        I[z] = true;
                        infectionDay[z] = day;
                    }
                }
            }
        }
    }
    vector<int> res(M);
    for (ll i = 0; i < M; i++)
    {
        if (infectionDay[i] == INF) res[i] = 0;
        else
        {
            res[i] = D[i] + pathSize[i] - infectionDay[i];
        }
    }
    return res;
}
/*

signed main() {
    int n, m;

    std::cin >> n;
    std::cin >> m;

    std::vector<std::pair<int, int>> edges = {};
    std::vector<long long> day;
    std::vector<int> start;
    std::vector<int> target;
    std::vector<bool> infected;

    for (int i = 0; i < n - 1; i++) {
        int u, v;
        std::cin >> u >> v;
        edges.emplace_back(u, v);
    }

    for (int i = 0; i < m; i++) {
        long long _d, _s, _t, _i;
        std::cin >> _d >> _s >> _t >> _i;
        day.push_back(_d);
        start.push_back(_s);
        target.push_back(_t);
        infected.push_back(_i);
    }

    const auto ans = solve(edges, day, start, target, infected);
    for (const auto it : ans) {
        std::cout << it << std::endl;
    }

    return 0;
}
/**/