#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using pi = pair<int, int>;
using vpi = vector<pi>;
template<class T> bool chmin(T& a, const T& b) {return b < a ? a = b, 1 : 0;}
template<class T> bool chmax(T& a, const T& b) {return a < b ? a = b, 1 : 0;}
#define bg(x) (x).begin()
#define en(x) (x).end()
#define all(x) bg(x), en(x)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rev(i, a, b) for (int i = a; i >= b; i--)
#define sz(x) int((x).size())
#ifdef LOCAL
#define dbg(x) cerr << #x << " = " << x << "\n"
#else
#define dbg(x)
#endif
vi paint(int n, vpi edges, int t) {
int m = sz(edges);
vector<vpi> G(n+1); rep(i, 0, m) G[edges[i].first].push_back({edges[i].second, i}), G[edges[i].second].push_back({edges[i].first, i});
vi colors(m), vis(n+1);
queue<int> q; q.push(t);
vis[t] = 1;
while (!q.empty()) {
int u = q.front(); q.pop();
for (auto [v, e] : G[u]) {
if (vis[v]) continue;
q.push(v);
vis[v] = 1;
colors[e] = (u<v) ? 1 : 0;
}
}
return colors;
}
int travel(int n, int u, vpi neighbours) {
for (auto [v, e] : neighbours) {
if ((u<v) != e) return v;
}
return neighbours[0].first;
}