Under construction

Still being built. Things may change

Seat Reservation

Interview

Time limit1sMemory limit1024 MB

Summary
Replay the seat applications in time order, granting a seat only when it is unreserved, and report each student's final seat by student number.
Level

Easy3 of 10

Topics
Sorting, Simulation
Solved
No attempts yet

Problem

Daejeon Science High School has a first-come, first-served system for reserving study seats.

There are XX students. Each student has a unique student number from 11 to XX. Each student can apply for one of CC seats. Each seat has a unique number from 11 to CC.

The system works as follows.

  • Each seat is either reserved or unreserved. Before any student applies, every seat is unreserved.
  • If a student applies for a seat that is already reserved, the application is invalid. An application that is not invalid is successful.
  • A student whose application succeeds is assigned that seat immediately, and the seat becomes reserved.
  • If a student who already has a seat succeeds in applying for another seat, the seat they had before becomes unreserved.

The system failed, so the applications were not processed correctly. Fortunately, the server kept a log of the applications. Each log entry has the time TT of the application, the seat number SS the student applied for, and the student number NN. A smaller TT means the application happened earlier.

Given the number of students and the log, write a program that finds the seat each student is finally assigned.

Input

The first line contains the number of students XX, the number of seats CC, and the number of log entries KK, separated by spaces. (1≤X,C,K≤100 000)(1 \le X, C, K \le 100\,000)

Each of the next KK lines contains an application time TT, the seat number SS, and the student number NN, separated by spaces. (0≤T≤1 000 000 000(0 \le T \le 1\,000\,000\,000, 1≤S≤C1 \le S \le C, 1≤N≤X)1 \le N \le X)

All application times TT are distinct. The log is not necessarily sorted by time.

Output

On each line, print the student number and the seat number that student is finally assigned, in increasing order of student number, separated by a space. Do not print students who did not apply for any seat or who never succeeded in applying for a seat.

Examples1

  1. Example 1

    Input
    3 4 5
    2 1 3
    1 1 2
    6 4 1
    4 2 2
    7 4 1
    
    Expected output
    1 4
    2 2