This page is still under construction.

Parts of this page are still being built. What you see may change.

Sunlight on a Tree

Time limit5sMemory limit256 MB

Summary
Report all nodes on the tree path from u to v whose dot product with the query direction is minimal.
Level

Hard8 of 10

Topics
Tree, Segment tree, Geometry
Solved
No attempts yet

Problem

Look down on a tree from directly above. Each branch is an edge and each point where branches meet or end is a node, so the top view is a graph drawn on the plane. Edges of the drawing may cross, but the graph itself is a tree: it is connected and has no cycle, and its nn nodes, each placed at a 2D coordinate, are joined by n−1n - 1 edges.

A query gives four numbers uu, vv, xx, yy. Here uu and vv are two nodes, and the vector (x,y)(x, y) is the direction the sunlight travels. The sunlight is not a single ray. It is a bundle of parallel rays that comes from infinitely far away and moves along (x,y)(x, y). The figure below draws (x,y)=(2,1)(x, y) = (2, 1): the light arrives from the lower left and every ray is parallel to (2,1)(2, 1).

Parallel rays travelling along the vector (2, 1)

A node is warmer when the light reaches it earlier. The rays travel along (x,y)(x, y), so the light reaches a node at (px,py)(p_x, p_y) earlier when xpx+ypyx p_x + y p_y is smaller. An ant walks from uu to vv along the unique shortest path of the tree. Report every node on that path that the sunlight reaches first, that is, every node of the path whose value of xpx+ypyx p_x + y p_y is minimum.

A tree with 14 nodes

The second figure shows a tree with 14 nodes. For u=14u = 14, v=4v = 4, x=1x = 1, y=0y = 0 the path is 14, 11, 1, 3, 4, the light comes from the left, and the sunlight reaches nodes 3 and 11 first. For u=13u = 13, v=9v = 9, x=1x = 1, y=−1y = -1 the path is 13, 11, 1, 6, 9, the light comes from the upper left, and the sunlight reaches nodes 1, 6 and 11 first.

Input

The first line contains the number of test cases TT (T≤10T \le 10). Each test case starts with a line holding the number of nodes nn (1≤n≤1051 \le n \le 10^5). The next nn lines hold the node coordinates, one node per line, and the ii-th of those lines gives the coordinates of node ii (∣x∣,∣y∣≤105|x|, |y| \le 10^5). Several nodes may sit at the same coordinates. Each of the next n−1n - 1 lines holds the ids of the two nodes that an edge connects, and an id is between 1 and nn. The input is always a valid tree. The next line holds the number of queries QQ (1≤Q≤1.6×1051 \le Q \le 1.6 \times 10^5). Each of the next QQ lines holds one query in the form uu vv xx yy (1≤u,v≤n1 \le u, v \le n, ∣x∣,∣y∣≤105|x|, |y| \le 10^5). The vector (x,y)(x, y) is never the zero vector, and uu and vv may be the same node. Not every test case is a worst case, but at least one of them uses the largest number of nodes and queries.

Output

For each test case print a line Case k:, where kk is the number of the test case counting from 1. Then print one line per query holding the ids of the nodes that the sunlight reaches first, sorted in increasing order and separated by single spaces. Do not put a space at the beginning or the end of a line. Over the whole input your program prints at most 3×1053 \times 10^5 ids.

Examples2

  1. Example 1

    Input
    2
    4
    0 0
    1 1
    0 1
    1 0
    1 2
    1 3
    1 4
    3
    2 3 0 -1
    2 3 0 1
    2 3 1 -1
    14
    0 0
    6 0
    -3 5
    -2 7
    -6 6
    4 4
    3 6
    7 5
    7 3
    4 2
    -3 -3
    -5 -1
    -4 -5
    0 -4
    1 2
    1 3
    3 4
    3 5
    1 6
    6 7
    6 8
    6 9
    6 10
    1 11
    11 12
    11 13
    11 14
    2
    14 4 1 0
    13 9 1 -1
    
    Expected output
    Case 1:
    2 3
    1
    3
    Case 2:
    3 11
    1 6 11
    
  2. Example 2

    Input
    3
    1
    0 0
    2
    1 1 1 0
    1 1 -7 99999
    2
    5 5
    -5 -5
    1 2
    5
    1 2 1 1
    2 1 1 1
    1 2 -1 -1
    1 1 3 -4
    2 2 0 1
    3
    0 0
    2 0
    1 3
    1 2
    1 3
    4
    2 3 0 1
    2 3 0 -1
    2 3 1 0
    3 2 -1 0
    
    Expected output
    Case 1:
    1
    1
    Case 2:
    2
    2
    1
    1
    2
    Case 3:
    1 2
    3
    1
    2