
#include <bits/stdc++.h>
using namespace std;

void bfs(vector<int> &dist, vector<vector<int>> &a, int start){
    queue<int> q;
    vector<int> vis(a.size()+1, 0);
    q.push(start);
    dist[start] = 0;
    while(!q.empty()){
        int p = q.front();
        for(auto &i : a[p]){
            if(vis[i] != 0) continue;
            q.push(i);
            dist[i] = dist[p]+1;
            vis[i] = 1;
        }
    }
}

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
){
    vector<vector<int>> a(R.size()+1);
    for(auto &i : R){
        a[i.first].push_back(i.second);
        a[i.second].push_back(i.first);
    }
    vector<int> dist(S.size(), 0);
    bfs(dist,a,0);
    vector<int> ans;
    for(int i = 0; i < S.size(); i++){
        if(I[i]){
            ans.push_back(abs(dist[T[i]]-dist[S[i]]));
        } else{
            ans.push_back(0);
        }
    }
    return ans;
}