Jabber Network
시간 제한2초메모리 제한1024 MB
오래된 케이블을 하나씩 제거한 뒤 통신 스트레스가 최소가 되도록 새 케이블로 트리를 다시 연결하고, 동률이면 끝점 번호가 가장 작은 쌍을 골라 각 단계의 연결 쌍을 출력한다.
문제
Dave, an old Computer Science professor, still maintains a local community computer network even after retirement. Each community member has a computer with three networking cards, and some of these cards may be connected by a cable. They form a connected network, and, following a long resource-saving tradition, the number of cables is kept to the minimum possible.
The habits of all the community members are quite stable: for every two computers the number of packets per second between them is known exactlyc. However, the network was first assembled a long time ago, so the connections are not necessarily be optimal any more. For two computers numbered and we define the shortest path between them, measured in the number of cables, and the number of packets per second that should be transferred from to . The commutation stress is defined to be the sum of for all , and one would like to minimise it.
Dave realised that it is finally the time to upgrade the cables --- after all, they do degrade with time. He wants to take this opportunity to also optimise the network, such that the commutation stress becomes smaller. However, he is no longer as quick as in his youth, and his friends may get dissatisfied if too much disruption happens at once. So he decided that he will perform the upgrade using the following scenario. For each of the old cables, he will do the following:
- Remove the old cable.
- Connect the network back using a new cable, choosing the computers to connect in such a way that the resulting commutation stress is minimum possible.
- If there are many ways to do this, break ties by choosing the computers with the smallest numbers: if and result in the same commutation stress, but (or and ), then should be chosen.
Figure J.1: A single reconnection operation (the first one in the sample input)
Note that, since each computer only has three network cards, Dave cannot connect two arbitrary computers on the second step: if one of them is already connected to three other computers, it is impossible to connect it to yet another computer. Fortunately, it is not hard to show that it is always possible to find two computers to connect: for instance, Dave can choose the two just-disconnected computers.
Unfortunately, the task appeared to be more difficult than it seemed initially. Could you help Dave?
입력
The first line of the input file contains an integer , the number of computers in the network.
The following lines contain two integers each: , , where are the numbers of the computers initially connected by an old cable number . The cables are to be removed and replaced in the order they are given in the input file. It is guaranteed that it is possible to reach any computer from any other computer using old cables (that is, the network is initially connected), and that no computer is connected with more than three other computers.
The next line contains an integer , the number of computer pairs that are known to transmit data to each other.
The following lines contain three integers each: , and , where and are the numbers of the computers which transmit data to each other , and is the number of packets per second to be transmitted.
출력
Output lines containing two integers each: , , where , should be the numbers of the computers connected by a new cable at step .
Note that, due to the tie-breaking rule detailed above, the correct output is unique.


