This page is still under construction.

Parts of this page are still being built. What you see may change.

Coin type identification

Time limit2sMemory limit512 MB

Summary
Determine each coin fixed type from the pairwise weighing results, printing ? when it is not unique.
Level

Medium7 of 10

Topics
Union-find, Topological sort, Dynamic programming, Graph
Solved
No attempts yet

Problem

Mirko visited a distant country where nobody uses banknotes, only coins. The country has NN types of coins in circulation, named K1, K2, K3, ..., KN. All coins have the same size and shape, but their weights differ. K1 is the lightest type, K2 is the second lightest, and so on up to KN, the heaviest type.

Mirko has MM coins in his pocket and does not know which type each one is. The only tool he has for finding out is a simple balance scale.

Mirko first labelled his unknown coins with the numbers 11 through MM, and then performed VV weighings. In one weighing he puts one coin on one side of the scale and another coin on the other side. He then sees whether the two coins weigh the same, and if they do not, which one is heavier.

Write a program that uses the weighing results to determine the type of every coin whose type is fixed by those results.

Input

The first line contains the integers NN, MM and VV: the number of coin types in the country, the number of coins in Mirko's pocket, and the number of weighings.

Each of the next VV lines holds the result of one weighing in the form ACB, where AA and BB are different positive integers not greater than MM, and CC is the character = (equal) or < (lighter).

There is no space between the numbers and the character CC. One weighing result says that Mirko's coin numbered AA weighs the same as the coin numbered BB, or is lighter than it.

The weighing results are never contradictory.

Output

Print MM lines. Line ii must contain the type of the coin numbered ii, written as KX, where XX is an integer between 11 and NN.

If the type of the coin numbered ii cannot be determined uniquely, print the character ? on line ii.

Constraints

In all subtasks, 2≤N≤300 0002 \le N \le 300\,000, 2≤M≤300 0002 \le M \le 300\,000 and 1≤V≤300 0001 \le V \le 300\,000.

Examples2

  1. Example 1

    Input
    3 5 3
    1<2
    2<4
    3=5
    
    Expected output
    K1
    K2
    ?
    K3
    ?
    
  2. Example 2

    Input
    2 7 6
    1=2
    2=3
    2=7
    3<4
    4=5
    4=6
    
    Expected output
    K1
    K1
    K1
    K2
    K2
    K2
    K1