The Cave
Time limit2sMemory limit512 MB
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.
Problem
A group of speleologists plans to explore a recently discovered cave complex. The cave complex consists of chambers numbered from to . The chambers are connected by 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 speleologists, numbered from to . Each speleologist has stated the area of the cave he wants to explore. Speleologist wants to begin his exploration in chamber , finish in chamber , and traverse at most 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 (), 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 and (), the number of chambers in the cave and the number of speleologists. The next lines describe the corridors. Each of them contains two integers and (), meaning that chambers and are connected by a direct corridor.
The next lines describe the speleologists' requirements. The -th of these lines contains three integers , , (, ): speleologist begins in chamber , finishes in chamber , and passes through at most corridors while moving between chambers. It is guaranteed that chamber can be reached from chamber by traversing at most corridors. The sum of over all test cases and the sum of over all test cases each do not exceed .
Output
Print exactly lines. The -th line contains the answer to the -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.