Under construction

Still being built. Things may change

Pairing

Time limit3sMemory limit128 MB

Summary
Given complete preference rankings of N men and N women, compute the man-optimal stable pairing.
Level

Medium5 of 10

Topics
Greedy, Simulation, Queue
Solved
No attempts yet

Problem

There are nn men and nn women to pair up. Every man is paired with exactly one woman, and every woman is paired with exactly one man. Each person ranks everyone on the other side from most preferred to least preferred, with no ties.

A pairing is stable when this holds: there is no man mm and woman ww, not paired with each other, such that mm prefers ww to his own partner and ww prefers mm to her own partner.

Several stable pairings can exist, so print only the man-optimal one. In the man-optimal pairing, every man is paired with the woman he prefers most among the women who are his partner in at least one stable pairing. This pairing always exists and it is unique.

Given every ranking, find that pairing.

Input

The first line contains the number of people NN (1≤N≤10001 \le N \le 1000). The men are numbered 1 to NN, and the women are numbered 1 to NN as well.

Line ii of the next NN lines holds the ranking of man ii. It lists every number from 1 to NN once, with the number of the more preferred woman earlier in the line.

Line jj of the following NN lines holds the ranking of woman jj in the same format.

Output

Print NN lines. Line ii holds the number of the woman paired with man ii in the man-optimal pairing.

Examples2

  1. Example 1

    Input
    4
    3 2 1 4
    2 4 1 3
    3 1 4 2
    1 2 3 4
    1 3 2 4
    3 4 2 1
    2 3 4 1
    4 2 1 3
    
    Expected output
    1
    4
    3
    2
    
  2. Example 2

    Input
    2
    1 2
    2 1
    2 1
    1 2
    
    Expected output
    1
    2