Ants

Simulate K ants that bounce off the walls of a W by H field and reverse on head-on meetings for T seconds, then report each ant position and direction.

Medium6MathSortingNo attempts yetTime limit2sMemory limit256 MB

Problem

A rectangular field is WW centimeters wide and HH centimeters tall, with one corner at (0,0)(0, 0) and the opposite corner at (W,H)(W, H). Inside the field, KK ants move along the coordinate grid lines at 1 centimeter per second. Each ant moves in one of four directions.

Ants are proud and never give way. When two ants meet head-on, both turn around on the spot and run back the way they came. Two ants moving perpendicular to each other ignore each other, even when they pass through the same point. An ant that reaches the boundary of the field also turns around and runs back.

Turning takes no time. If a head-on meeting or a boundary touch happens exactly at second TT, report the direction the ant has after the turn.

You are given the starting position and direction of every ant. Find the position and direction of each ant after TT seconds.

Input

The first line contains four integers WW, HH, KK, TT (1W,H,K1001 \le W, H, K \le 100, 1T1091 \le T \le 10^9).

Each of the next KK lines contains three integers XiX_i, YiY_i, DiD_i, where (Xi,Yi)(X_i, Y_i) is the position of the ii-th ant and DiD_i is the direction it moves in (0<Xi<W0 < X_i < W, 0<Yi<H0 < Y_i < H, 1Di41 \le D_i \le 4). Di=1D_i = 1 means the ant moves toward increasing XX, Di=2D_i = 2 toward increasing YY, Di=3D_i = 3 toward decreasing XX, and Di=4D_i = 4 toward decreasing YY.

Numbers on one line are separated by single spaces. No two ants start at the same point.

Output

Print KK lines, one for each ant, in the same order as the input. Each line holds three integers separated by single spaces: the coordinates of the ant after TT seconds and the direction it moves in.