#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define all(x) (x).begin(),(x).end()

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

using ll = long long;
#define int ll
#warning :(( long longul vostru gen e so nenecesar
#define sz(x) ((int)(x).size())
using pii = pair<int, int>;
using tii = tuple<int, int, int>;

const int nmax = 15e4 + 5;

bool isinfected[nmax];
int wheninfected[nmax];
int rez[nmax];


vector<int> g[nmax];
namespace Tree {
	int d[nmax];
	int p[nmax], jump[nmax];
	int pin[nmax], pout[nmax], inp;
	
	void init(int node, int f) {
		d[node] = d[f] + 1;
		p[node] = jump[node] = f;
		if(d[jump[jump[f]]] + d[f] == 2 * d[jump[f]]) jump[node] = jump[jump[f]];
		pin[node] = inp++;
		
		for(auto x : g[node]) {
			if(x == f) continue;
			init(x, node);
		}
		
		pout[node] = inp - 1;
	}
	
	bool isanc(int a, int b) {
		return pin[a] <= pin[b] && pout[b] <= pout[a];
	}
	
	int lca(int a, int b) {
		while(!isanc(a, b)) {
			if(!isanc(jump[a], b)) a = jump[a];
			else a = p[a];
		}
		return a;
	}
	int dist(int a, int b) {
		return d[a] + d[b] - 2 * d[lca(a, b)];
	}
}

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
) {
	int M = sz(D);
	for(auto [a, b] : R) g[a].emplace_back(b), g[b].emplace_back(a);
	Tree::init(1, 1);
	
	for(int i = 0; i < M; i++) {
		isinfected[i] = I[i];
		if(I[i]) rez[i] = Tree::dist(S[i], T[i]) + 1;
	}
		
	vector<int> result;
	for(int i = 0; i < M; i++) {
		result.emplace_back(rez[i]);
	}
	return result;
}

	
