#include "infection.h"
#include "bits/stdc++.h"

#define pii pair<int, int>
#define pb push_back
#define ar array
#define ll long long

const ll inf = 1e18 + 7;

using namespace std;

vector<int> solve(
    vector<pair<int, int>> R, vector<long long> D, vector<int> S, vector<int> T, vector<bool> I) {

    int n = R.size() + 1;

    vector<vector<int>> pon(n);

    for (auto [u, v]: R) {
        pon[u].pb(v);
        pon[v].pb(u);
    }

    int m = D.size();

    vector<vector<int>> paths(m);

    vector<ll> days;

    for (int i = 0; i < m; i++) {
        bool flag = 0;

        function<void(int, int, int)> dfs = [&](int u, int p, int f) {
            paths[i].pb(u);

            if (u == f) {
                flag = 1;

                return;
            }

            for (auto v: pon[u]) {
                if (v != p) {
                    dfs(v, u, f);
                }

                if (flag) {
                    return;
                }
            }

            if (!flag) {
                paths[i].pop_back();
            }
        };

        dfs(S[i], S[i], T[i]);
    }

    vector<int> ret;

    for (int i = 0; i < m; i++) {
        ret.pb(I[i] ? paths[i].size() : 0);
    }

    return ret;
}

/*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;
}*/


