This page is still under construction.

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

The Cave

Time limit2sMemory limit512 MB

Summary
On a tree, decide whether one chamber lies on some walk from a_i to b_i using at most d_i edges, for every speleologist, and output the smallest such chamber.
Level

Hard8 of 10

Topics
Tree, Graph, Greedy, DFS
Solved
No attempts yet

Problem

A group of speleologists plans to explore a recently discovered cave complex. The cave complex consists of nn chambers numbered from 11 to nn. The chambers are connected by n−1n-1 corridors in such a way that any chamber can be reached from any other. Each corridor connects exactly two chambers.

The cave will be explored by a group of mm speleologists, numbered from 11 to mm. Each speleologist has stated the area of the cave he wants to explore. Speleologist ii wants to begin his exploration in chamber aia_i, finish in chamber bib_i, and traverse at most did_i corridors on his way (every pass through a corridor is counted separately, even when the same corridor is used again). Byteasar, the head of the expedition, wants all the researchers to meet at some point in time to exchange their observations. He is wondering whether he can choose one chamber of the cave and plan the routes of all speleologists so that every route passes through the selected chamber. The planned routes must meet the requirements stated by the researchers.

Input

The first line contains one integer tt (1≤t≤10001 \le t \le 1000), the number of test cases. The descriptions of the test cases follow. The description of a single test case begins with a line containing two integers nn and mm (2≤n,m≤300 0002 \le n, m \le 300\,000), the number of chambers in the cave and the number of speleologists. The next n−1n-1 lines describe the corridors. Each of them contains two integers uiu_i and wiw_i (1≤ui,wi≤n1 \le u_i, w_i \le n), meaning that chambers uiu_i and wiw_i are connected by a direct corridor.

The next mm lines describe the speleologists' requirements. The ii-th of these lines contains three integers aia_i, bib_i, did_i (1≤ai,bi≤n1 \le a_i, b_i \le n, 1≤di≤600 0001 \le d_i \le 600\,000): speleologist ii begins in chamber aia_i, finishes in chamber bib_i, and passes through at most did_i corridors while moving between chambers. It is guaranteed that chamber bib_i can be reached from chamber aia_i by traversing at most did_i corridors. The sum of nn over all test cases and the sum of mm over all test cases each do not exceed 300 000300\,000.

Output

Print exactly tt lines. The ii-th line contains the answer to the ii-th test case. If the routes can be planned so that they all pass through one common chamber, print the word TAK (Polish for yes) followed by a space and the number of the chamber where the meeting takes place. Otherwise print only the word NIE (Polish for no). If several chambers are valid meeting places, print the one with the smallest number.

Examples3

  1. Example 1

    Input
    2
    5 3
    1 2
    2 3
    2 4
    3 5
    1 4 2
    5 5 5
    3 2 1
    3 2
    1 2
    2 3
    1 1 2
    3 3 1
    
    Expected output
    TAK 2
    NIE
    
  2. Example 2

    Input
    2
    7 2
    4 1
    4 2
    4 3
    2 5
    2 6
    6 7
    1 3 4
    5 7 5
    2 2
    1 2
    1 2 1
    2 1 3
    
    Expected output
    TAK 2
    TAK 1
    
  3. Example 3

    Input
    2
    4 3
    1 2
    2 3
    3 4
    1 1 3
    4 4 4
    3 3 2
    4 2
    1 2
    2 3
    3 4
    1 1 1
    4 4 1
    
    Expected output
    TAK 2
    NIE