#U2122FS1. Redistributing Gifts

Redistributing Gifts

Farmer John has NN gifts labeled 1N1…N for his NN cows, also labeled 1N1…N (1N5001≤N≤500). Each cow has a wishlist, which is a permutation of all NN gifts such that the cow prefers gifts that appear earlier in the list over gifts that appear later in the list.FJ was lazy and just assigned gift ii to cow ii for all ii. Now, the cows have gathered amongst themselves and decided to reassign the gifts such that after reassignment, every cow ends up with the same gift as she did originally, or a gift that she prefers over the one she was originally assigned.

For each ii from 11 to NN, compute the most preferred gift cow ii could hope to receive after reassignment.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains NN. The next NN lines each contain the preference list of a cow. It is guaranteed that each line forms a permutation of 1N1…N.

OUTPUT FORMAT (print output to the terminal / stdout):

Please output NN lines, the ii-th of which contains the most preferred gift cow ii could hope to receive after reassignment.

SAMPLE INPUT:

4
1 2 3 4
1 3 2 4
1 2 3 4
1 2 3 4

SAMPLE OUTPUT:

1
3
2
4

In this example, there are two possible reassignments:

  • The original assignment: cow 11 receives gift 11, cow 22 receives gift 22, cow 33 receives gift 33, and cow 44 receives gift 44.
  • Cow 11 receives gift 11, cow 22 receives gift 33, cow 33 receives gift 22, and cow 44 receives gift 44.

Observe that both cows 11 and 44 cannot hope to receive better gifts than they were originally assigned. However, both cows 22 and 33 can.

SCORING:

  • Test cases 2-3 satisfy N8N≤8.
  • Test cases 4-11 satisfy no additional constraints.