#include using namespace std; using ll = long long; struct unionfind{ int n; vector parent; vector num; unionfind(int n):n(n),parent(vector(n)),num(vector(n,1)){ for(int i = 0; i < n; i++) parent[i] = i; } int find(int n){ if (parent[n] == n) return n; parent[n] = find(parent[n]); return parent[n]; } int merge(int n,int m){ n = find(n); m = find(m); if (n==m) return n; if (num[n]>num[m]) return merge(m,n); parent[m] = n; num[n] += num[m]; return n; } int getnum(int n){ return num[find(n)]; } }; ll d[2<<17],depth[2<<17]; ll par[2<<17][18]; ll pp[2<<17]; int n,x,q; vector> g[2<<17]; unionfind uf(2<<17); vector use; pair d1(int ni,int p,ll w){ pair now = make_pair(0ll,0ll); depth[ni] = depth[p] + w; d[ni] = d[p] + 1; par[ni][0] = p; for(int i = 1;i<=17;i++){ par[ni][i] = par[ni][i-1]; if(par[ni][i] == -1) continue; par[ni][i] = par[par[ni][i]][i-1]; } for(auto&to:g[ni]) if(to.first!=p) { pair nxt = d1(to.first,ni,to.second); now.first = max(now.first,max(nxt.first,nxt.second + now.second)); now.second = max(nxt.second,now.second); } now.second += w; return now; } map vis[2<<17]; int main(){ vector>sum(2<<17); vector>nnn(2<<17); vector>mmm(2<<17); vector aa(2<<17,0); cin>>n>>x>>q; for(int i = 0;i>op; if(op==1){ int u; ll w; cin>>u>>w; int v = x; if(uf.getnum(u) mx = d1(v,u,w); int kk = pp[uf.find(u)]; uf.merge(u,v); pp[uf.find(u)] = kk; int pre = kk; int nu = u; if(pre==u){ sum[pre].insert(mx.second); nnn[pre][v] = mx.second; aa[pre] = max(mx.first,mx.second); continue; } int now = 17; while(now){ if(par[u][now]!=-1&&par[u][now]!=pre) u = par[u][now]; now--; } assert(u!=pre); ll nxt = max(nnn[pre][u],depth[nu]+mx.second); if(vis[pre][u]++) sum[pre].erase(sum[pre].find(nnn[pre][u])); nxt = aa[pre]; nxt = max(nxt,mx.first); if(sum[pre].size()){ auto itr = sum[pre].end(); itr--; nxt = max(nxt,*itr+mx.second+depth[nu]); } aa[pre] = nxt; sum[pre].insert(mx.second+depth[nu]); nnn[pre][u] = mx.second+depth[nu]; }else if(op==2){ int u,v; cin>>u>>v; int nu = u; int nv = v; if(uf.find(u)!=uf.find(v)){ cout<<-1<d[v]) swap(u,v); ll need = d[v] - d[u]; int now = 0; while(need){ if(need&1) v = par[v][now]; now++; need >>= 1; } assert(d[u]==d[v]); now = 17; while(now){ if(par[u][now]!=par[v][now]){ u = par[u][now]; v = par[v][now]; } now--; } if(u!=v) u = par[u][0]; ll ans = 2*depth[u] - depth[nu] - depth[nv]; ans *= -1; cout<>u; int pre = pp[uf.find(u)]; cout<>y; y %= n; x += y; x %= n; } } }