import sys from sys import stdin def main(): sys.setrecursionlimit(1 << 25) N = int(stdin.readline()) adj = [[] for _ in range(N+1)] # 1-based for _ in range(N-1): a, b = map(int, stdin.readline().split()) adj[a].append(b) adj[b].append(a) val = list(range(N+1)) # val[i] is the value of node i Q = int(stdin.readline()) prev_x = 0 for _ in range(Q): ui, vi = map(int, stdin.readline().split()) # Compute encrypted u and v mod = N u = ((ui + (N - 1) + prev_x) % mod) + 1 v = ((vi + (N - 1) + prev_x) % mod) + 1 # Swap values val[u], val[v] = val[v], val[u] # Simulate the path to find ans current = u prev_node = -1 ans = current while True: next_nodes = [node for node in adj[current] if node != prev_node] if not next_nodes: break # Find max val and the candidates max_val = -1 candidates = [] for node in next_nodes: if val[node] > max_val: max_val = val[node] candidates = [node] elif val[node] == max_val: candidates.append(node) # Choose the candidate with largest node number next_node = max(candidates) prev_node = current current = next_node ans = current print(ans) prev_x = ans if __name__ == "__main__": main()