PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000050. Rạp xiếc của những chú bọ chét (Flea Circus)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Rạp xiếc của những chú bọ chét (Flea Circus)

Flea Circus

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Two fleas start at vertices aa and bb of an undirected tree. They simultaneously jump toward each other along their unique shortest path, traversing one edge per jump.

If the path contains an even number of edges, they meet at one vertex. Otherwise, they eventually keep jumping back and forth between two adjacent vertices. For each starting pair, report the meeting vertex or the two alternating vertices, listed in ascending order.

Input

  • The first line contains nn.
  • The next n−1n-1 lines contain edges u,vu,v.
  • The next line contains qq.
  • The following qq lines contain starting positions a,ba,b.

The original problem ends a sequence of datasets with n=0n=0; the PHOJ interface uses the single dataset described above.

Output

For each query print exactly one of:

  • The fleas meet at x. if the fleas meet at vertex xx.
  • The fleas jump forever between x and y. if they alternate between x<yx<y.

Preserve capitalization, spaces, and the terminal period.

Subtasks

General constraints: 1≤n≤50001\le n\le5000; 1≤q≤5001\le q\le500; 1≤a,b,u,v≤n1\le a,b,u,v\le n.

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40; 1≤q≤501\le q\le 50.

  • Subtask 2 (30 points): 1≤n≤10001\le n\le 1000; 1≤q≤5001\le q\le 500.

  • Subtask 3 (50 points): 1≤n≤50001\le n\le 5000; 1≤q≤5001\le q\le 500.

Examples

Input

8
1 2
1 3
2 4
2 5
3 6
3 7
5 8
5
5 1
7 4
1 8
4 7
7 8

Output

The fleas meet at 2.
The fleas meet at 1.
The fleas jump forever between 2 and 5.
The fleas meet at 1.
The fleas jump forever between 1 and 2.

Explanation

For (5,1)(5,1), the path 5−2−15-2-1 has two edges so the fleas meet at 22. The path 7−3−1−2−47-3-1-2-4 yields a meeting at 11. The path 1−2−5−81-2-5-8 has two central adjacent vertices 2,52,5. Query (4,7)(4,7) meets at 11; on 7−3−1−2−5−87-3-1-2-5-8, the two middle vertices are 1,21,2.