//Created By Mayur Agarwal :)
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <set>
#include <algorithm>
#include <map>
#include <iterator>
#include <functional>
#include <queue>
#define ll long long
#define ind(a) scanf("%d",&a)
#define in(a) scanf("%lld",&a)
#define inc(a) scanf("%c",&a)
#define ins(a) scanf("%s",a)
#define pr(a) printf("%lld\n",a)
#define debug(x) cout << #x << " = " << x << endl
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define pb push_back
#define ff first
#define ss second
#define SIZE 100010
const int mod = 10000007;
using namespace std;
typedef pair<int, int>pll;
int dp[19][(1 << 19)], h[SIZE], sz[SIZE], par[SIZE];
set<int>S[SIZE];
/*Using centroid Decomposition of a tree */
/*----------- Pre-Processing ------------*/
inline void dfs(int v = 0, int p = -1)
{
if (~p)
h[v] = h[p] + 1;
dp[0][v] = p;
for (int i = 1; i <= 18; i++)
{
if (~dp[i - 1][v])
dp[i][v] = dp[i - 1][dp[i - 1][v]];
}
for (auto it = S[v].begin(); it != S[v].end(); it++)
{
int u = *it;
if (u != p)
dfs(u, v);
}
}
inline int lca(int v, int u)
{
if (h[v] < h[u])
swap(v, u);
for (int i = 18; i >= 0; --i)
{
if (~dp[i][v] && h[dp[i][v]] >= h[u])
v = dp[i][v];
}
if (v == u)
return v;
for (int i = 18; i >= 0; --i)
{
if (dp[i][v] - dp[i][u])
{
v = dp[i][v];
u = dp[i][u];
}
}
return dp[0][v];
}
inline int dist(int v, int u)
{
return h[v] + h[u] - 2 * h[lca(v, u)];
}
/*-----------------Decomposition part--------------------------*/
int nn;
inline void dfs1(int v, int p)
{
nn++;
sz[v] = 1;
for (auto it = S[v].begin(); it != S[v].end(); it++)
{
int u = *it;
if (u != p)
{
dfs1(u, v);
sz[v] += sz[u];
}
}
}
inline int dfs2(int v, int p)
{
for (auto it = S[v].begin(); it != S[v].end(); it++)
{
int u = *it;
if (u != p && sz[u] > nn / 2)
{
return dfs2(u, v);
}
}
return v;
}
inline void decompose(int v = 0, int p = -1)
{
nn = 0;
dfs1(v, v);
int centroid = dfs2(v, v);
if (p == -1)
p = centroid;
par[centroid] = p;
for (auto it = S[centroid].begin(); it != S[centroid].end(); it++)
{
int u = *it;
S[u].erase(centroid);
decompose(u, centroid);
}
S[centroid].clear();
}
/*----------------- Handle the Queries -----------------*/
set<pll>q[SIZE];
set<pll>::iterator iter;
bool white[SIZE];
inline void update(int u)
{
int x = u;
while (1)
{
q[x].insert(pll(u, dist(x, u)));
if (x == par[x])
break;
x = par[x];
}
}
inline int distW(int x)
{
while (!q[x].empty())
{
iter = q[x].begin();
if (!white[(*iter).ff])
q[x].erase(q[x].begin());
else
{
return (*iter).ss;
}
}
return mod;
}
inline int query(int u)
{
int x = u;
int res = mod;
while (1)
{
res = min(res, distW(x) + dist(u, x));
if (x == par[x])
break;
x = par[x];
}
return res;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
for (int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
u--;
v--;
S[u].insert(v);
S[v].insert(u);
}
dfs();
decompose();
int q;
cin >> q;
while (q--)
{
int typ, v;
cin >> typ >> v;
v--;
if (typ == 0)
{
if (white[v])
{
white[v] = 0;
}
else
{
white[v] = 1;
}
if (white[v])
update(v);
}
else
{
int res = query(v);
if (res >= mod)
{
cout << "-1\n";
}
else
{
cout << res << endl;
}
}
}
return 0;
}
Ly9DcmVhdGVkIEJ5IE1heXVyIEFnYXJ3YWwgOikKCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxjbWF0aD4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxzZXQ+CiNpbmNsdWRlIDxhbGdvcml0aG0+Ci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