Masonry Bridge

Given a connected undirected graph with edge weights, find the minimum over edge orderings of the earliest time island 1 and island N connect, and the maximum such time.

Medium7GraphMinimum spanning treeUnion-findGreedyNo attempts yetTime limit2sMemory limit128 MB

Problem

Dahyun and Jeongyeon live in Yeoul village, which has NN islands. Jeongyeon lives on the westernmost island and Dahyun lives on the easternmost one. They numbered the islands from 11 to NN, giving number 11 to Jeongyeon's island and number NN to Dahyun's island.

Rowing a boat between the two islands is inconvenient, so they asked Seongyeol, an architect who lives on island 33, to build masonry bridges that connect the NN islands.

Seongyeol planned to build MM bridges. Once all MM bridges are built, the NN islands form one connected group. No two bridges overlap anywhere in the middle, and no bridge passes through an island other than the two at its ends. Building bridge kk takes TkT_k units of time.

Seongyeol builds the bridges one at a time, in an arbitrary order. At some moment during the construction, Dahyun and Jeongyeon can cross between their islands over the bridges. The time elapsed at that moment is the sum of the construction times of the bridges built so far.

Consider the village in the picture above. If Seongyeol builds the bridges in index order 1,2,,91, 2, \dots, 9, islands 11 and NN become connected after six bridges, which is 1717 units of time. If he builds bridge 11 and then bridge 66, they become connected after 66 units of time. No order connects them within 55 units of time. If he builds the bridges in the order 1,9,7,5,8,3,2,6,41, 9, 7, 5, 8, 3, 2, 6, 4, they become connected only after six bridges, which is 2222 units of time, and once 2222 units of time have passed islands 11 and NN are connected whatever order he used.

Given the islands and the bridges, write a program that finds the smallest and the largest possible moment at which islands 11 and NN become connected.

Input

The first line contains the number of islands NN (4N500004 \le N \le 50000).

Each of the next NN lines contains the coordinates XkX_k and YkY_k of one island (1Xk,Yk10000001 \le X_k, Y_k \le 1000000, X1<Xk<XNX_1 < X_k < X_N).

The next line contains the number of bridges MM that Seongyeol plans to build (N1M1000000N - 1 \le M \le 1000000).

Each of the next MM lines contains the numbers SkS_k and EkE_k of the two islands that bridge kk connects, and its construction time TkT_k (SkEkS_k \ne E_k, 1Tk100001 \le T_k \le 10000).

Apart from island 11, no island has an xx coordinate at most X1X_1, and apart from island NN, no island has an xx coordinate at least XNX_N. No two islands sit at exactly the same position. At most one bridge connects any pair of islands.

Output

Print the smallest and the largest possible moment at which islands 11 and NN become connected, separated by a space.