Niebieskie ksi.eczki
III Olympiad in Informatics 1995/1996

Task: AGE
Author: Marcin Kubica
Agents

III stage contest  

The country is overwhelmed by agents from foreign secret services. Not only do they steel secret information, but also spy on each other. We say that an agent A unmasked an agent B, if A collected sufficient documents to arrest an agent B. Some agents take bribes - for the certain amount of money they are ready to hand over all the documents they have. So when we buy certain agents, we may initiate a chain of arrests (if we arrest an agent we take all his documents) which will lead to a liquidation of all the agents in the country.

Our home counterintelligence provided us with the number of foreign agents in the country, the information who and at what price may be corrupted, as well as what agents unmasked whom. The number of agents is equal to n <= 3000; and they have numbers from 1 to n.

Task

Write a program which:

Input

Output

Example

For the input file AGE.IN:

3
2
1 10
2 100
2
1 3
2 3

your program should produce the output file AGE.OUT:

TAK
110

For the input file AGE.IN:

4
2
1 100
4 200
2
1 2
3 4

your program should produce the output file AGE.OUT:

NIE
3

Your program should look for the file AGE.IN in the current directory and produce the output file AGE.OUT in the current directory too. The file containing the source code of your program should have a name AGE.???, whereas you should put three-letter abbreviation of the used programming language name instead of ???. The same program in executable form should be written in the file AGE.EXE