Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (x_{k}, y_{k}).

The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. [Image] 

Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!


-----Input-----

The first line of the input contains fix integers n, m, x, y, z, p (1 ≤ n, m ≤ 10^9; 0 ≤ x, y, z ≤ 10^9; 1 ≤ p ≤ 10^5).

Each of the following p lines contains two integers x_{k}, y_{k} (1 ≤ x_{k} ≤ n; 1 ≤ y_{k} ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.


-----Output-----

For each of the p candies, print on a single line its space-separated new coordinates.


-----Examples-----
Input
3 3 3 1 1 9
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

Output
1 3
1 2
1 1
2 3
2 2
2 1
3 3
3 2
3 1



-----Note-----

Just for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:

QWER      REWQ 

ASDF  ->  FDSA

ZXCV      VCXZ

Source: codeforces | Difficulty: MEDIUM
Tags: Mathematics

Implementation


Solutions:
from sys import stdin

def candy(a, b, c, d, e):
	if e == 0:
		return (a, b, c, d)
	if e == 1:
		return (b, c - a, d, c)
	if e == 2:
		return (c - a, d - b, c, d)
	return (d - b, a, d, c)

def candy2(a, b, c, e):
	if e == 0:
		return (a, b)
	if e == 1:
		return (a, c - b)

def main():
	inp = stdin
	(n, m, x, y, z, p) = map(int, inp.readline().split())
	(n, m, x, y, z) = (n + 1, m + 1, x % 4, y % 2, (4 - z) % 4)
	for i in range(0, p):
		(u, v) = map(int, inp.readline().split())
		(u, v, q, p) = candy(u, v, n, m, x)
		(u, v) = candy2(u, v, p, y)
		(u, v, q, p) = candy(u, v, q, p, z)
		print(u, v)
main()


(h, w, x, y, z, p) = map(int, input().split())
x %= 4
y %= 2
z %= 4

def rot(a, b):
	nonlocal h
	nonlocal w
	res = (b, h - a + 1)
	(w, h) = (h, w)
	return res

def flip(a, b):
	return (a, w - b + 1)
for i in range(p):
	(a, b) = map(int, input().split())
	(sh, sw) = (h, w)
	for i in range(x):
		(a, b) = rot(a, b)
	for i in range(y):
		(a, b) = flip(a, b)
	for i in range(3 * z):
		(a, b) = rot(a, b)
	print(a, b)
	(h, w) = (sh, sw)


def rotateClk(x, y, n):
	tmp = x
	x = y
	y = n + 1 - tmp
	return (x, y)

def rev(x, y, m):
	return (x, m + 1 - y)
(n, m, x, y, z, p) = [int(x) for x in input().split()]
pts = []
for _ in range(p):
	pts.append([int(x) for x in input().split()])
x = x % 4
y = y % 2
z = z % 4
for (i, pt) in enumerate(pts):
	if x == 1:
		pt = rotateClk(pt[0], pt[1], n)
	elif x == 2:
		pt = rotateClk(pt[0], pt[1], n)
		pt = rotateClk(pt[0], pt[1], m)
	elif x == 3:
		pt = rotateClk(pt[0], pt[1], n)
		pt = rotateClk(pt[0], pt[1], m)
		pt = rotateClk(pt[0], pt[1], n)
	pts[i] = pt
for (i, pt) in enumerate(pts):
	if y == 1:
		pts[i] = rev(pt[0], pt[1], m if x % 2 == 0 else n)
for (i, pt) in enumerate(pts):
	if z == 3:
		pt = rotateClk(pt[0], pt[1], n if x % 2 == 0 else m)
	elif z == 2:
		prev = n if x % 2 == 0 else m
		pt = rotateClk(pt[0], pt[1], prev)
		pt = rotateClk(pt[0], pt[1], n if prev == m else m)
	elif z == 1:
		prev = n if x % 2 == 0 else m
		pt = rotateClk(pt[0], pt[1], prev)
		pt = rotateClk(pt[0], pt[1], n if prev == m else m)
		pt = rotateClk(pt[0], pt[1], prev)
	pts[i] = pt
for pt in pts:
	print(pt[0], pt[1])


(R, C, x, y, z, p) = map(int, input().split())
tmpR = R
tmpC = C
x %= 4
y %= 2
z %= 4
z = 4 - z
for k in range(p):
	(i, j) = map(int, input().split())
	for t in range(x):
		tmpi = i
		i = j
		j = R - tmpi + 1
		(R, C) = (C, R)
	for t in range(y):
		j = C - (j - 1)
	for t in range(z):
		tmpi = i
		i = j
		j = R - tmpi + 1
		(R, C) = (C, R)
	print(i, end=' ')
	print(j)
	R = tmpR
	C = tmpC


import sys
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
(n, m, x, y, z, p) = (int(i) for i in input().split())
points = []
for i in range(0, p):
	points.append([int(j) for j in input().split()])
x %= 4
y %= 2
z %= 4
for i in range(0, x):
	for i in range(0, p):
		point = [0, 0]
		point[1] = n - points[i][0] + 1
		point[0] = points[i][1]
		points[i] = point
	(n, m) = (m, n)
if y == 1:
	for i in range(0, p):
		points[i][1] = m - points[i][1] + 1
for i in range(0, z):
	for i in range(0, p):
		point = [0, 0]
		point[1] = points[i][0]
		point[0] = m - points[i][1] + 1
		points[i] = point
	(n, m) = (m, n)
for i in range(0, p):
	print('%d %d\n' % (points[i][0], points[i][1]), end='')


tr = [[1, 0], [0, 1], [-1, 0], [0, -1]]
tc = [[0, 1], [-1, 0], [0, -1], [1, 0]]
(n, m, x, y, z, p) = map(int, input().split())
y %= 2
d = (x + (z if y else -z)) % 4
(r0, r1, c0, c1) = (tr[d][0], tr[d][1], tc[d][0] * (-1) ** y, tc[d][1] * (-1) ** y)
for pi in range(p):
	(i, j) = map(int, input().split())
	vi = [0, i, n - i + 1][r0] + [0, j, m - j + 1][r1]
	vj = [0, i, n - i + 1][c0] + [0, j, m - j + 1][c1]
	print(vi, vj)


(n, m, x, y, z, p) = map(int, input().split())
y = y % 2
x = x % 4
z = z % 4
for i in range(p):
	(xi, yi) = map(int, input().split())
	t = (n, m)
	for j in range(x):
		(xi, yi) = (yi, n - xi + 1)
		(n, m) = (m, n)
	if y == 1:
		yi = m - yi + 1
	for j in range(-z % 4):
		(xi, yi) = (yi, n - xi + 1)
		(n, m) = (m, n)
	(n, m) = (t[0], t[1])
	print(xi, yi)


(n, m, x, y, z, p) = map(int, input().split())
x = x % 4
y = y % 2
z = z % 4
(n0, m0) = (n, m)
for i in range(p):
	(n, m) = (n0, m0)
	(x1, y1) = map(int, input().split())
	for j in range(x):
		(x1, y1) = (y1, n - x1 + 1)
		(n, m) = (m, n)
	if y == 1:
		y1 = m - y1 + 1
	for i in range(z):
		(x1, y1) = (m - y1 + 1, x1)
		(n, m) = (m, n)
	print(x1, y1)


(n, m, x, y, z, p) = list(map(int, input().split()))
(n, m, x, y, z) = (n + 1, m + 1, x % 4, y % 2, (4 - z) % 4)

def a(i, j, n, m, k):
	if k == 0:
		return (i, j, n, m)
	if k == 1:
		return (j, n - i, m, n)
	if k == 2:
		return (n - i, m - j, n, m)
	return (m - j, i, m, n)

def b(i, j, m, k):
	if k == 0:
		return (i, j)
	if k == 1:
		return (i, m - j)
for i in range(p):
	(u, v) = list(map(int, input().split()))
	(u, v, q, p) = a(u, v, n, m, x)
	(u, v) = b(u, v, p, y)
	(u, v, q, p) = a(u, v, q, p, z)
	print(u, v)


from sys import stdin

def arr_inp(n):
	if n == 1:
		return [int(x) for x in stdin.readline().split()]
	elif n == 2:
		return [float(x) for x in stdin.readline().split()]
	else:
		return [str(x) for x in stdin.readline().split()]

def clockwise(mod):
	for i in range(1, mod + 1):
		for j in range(p):
			if i & 1:
				a[j] = [a[j][1], n + 1 - a[j][0]]
			else:
				a[j] = [a[j][1], m + 1 - a[j][0]]

def horizontal(mod):
	if mod:
		for i in range(p):
			if x & 1:
				a[i][1] = n + 1 - a[i][1]
			else:
				a[i][1] = m + 1 - a[i][1]

def counter(mod):
	for i in range(1, mod + 1):
		for j in range(p):
			if x % 2:
				if i & 1:
					a[j] = [a[j][1], m + 1 - a[j][0]]
				else:
					a[j] = [a[j][1], n + 1 - a[j][0]]
			elif i & 1:
				a[j] = [a[j][1], n + 1 - a[j][0]]
			else:
				a[j] = [a[j][1], m + 1 - a[j][0]]

def print1():
	for i in range(p):
		print(*a[i])
(n, m, x, y, z, p) = arr_inp(1)
a = [arr_inp(1) for i in range(p)]
clockwise(x % 4)
horizontal(y % 2)
if z % 4 > 0:
	counter(4 - z % 4)
print1()


(h, w, x, y, z, p) = list(map(int, input().split()))
(x, y, z) = (x % 4, y % 2, z % 4)

def rotation(a, b):
	nonlocal h, w
	res = (b, h - a + 1)
	(w, h) = (h, w)
	return res

def flip(a, b):
	return (a, w - b + 1)
res = []
for i in range(p):
	(a, b) = list(map(int, input().split()))
	(ch, cw) = (h, w)
	for i in range(x):
		(a, b) = rotation(a, b)
	for i in range(y):
		(a, b) = flip(a, b)
	for i in range(3 * z):
		(a, b) = rotation(a, b)
	res += [str(a) + ' ' + str(b)]
	(h, w) = (ch, cw)
print('\n'.join(map(str, res)))


(n, m, x, y, z, p) = map(int, input().split())
ps = []
for i in range(p):
	ps.append(list(map(int, input().split())))
x = x % 4
y = y % 2
z = z % 4 * 3 % 4
for p in ps:
	(mm, nn) = (m, n)
	(i, j) = p
	for _ in range(x):
		(i, j) = (j, nn - i + 1)
		(mm, nn) = (nn, mm)
	for _ in range(y):
		(i, j) = (i, mm - j + 1)
	for _ in range(z):
		(i, j) = (j, nn - i + 1)
		(mm, nn) = (nn, mm)
	print(i, j)


(n, m, x, y, z, p) = list(map(int, input().split()))
x %= 4
y %= 2
z %= 4
(tn, tm) = (n, m)
while p >= 1:
	(n, m) = (tn, tm)
	p -= 1
	(sx, sy) = list(map(int, input().split()))
	for i in range(0, x):
		(sx, sy) = (sy, n - sx + 1)
		(n, m) = (m, n)
	if y > 0:
		sy = m - sy + 1
	for i in range(0, z):
		(sx, sy) = (m - sy + 1, sx)
		(n, m) = (m, n)
	print(sx, sy)


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
(R, C, x, y, z, p) = value()
x = x % 4
y = y % 2
z = z % 4
for i in range(p):
	(i, j) = value()
	(r, c) = (R, C)
	for _ in range(x):
		(i, j) = (j, r - i + 1)
		(r, c) = (c, r)
	for _ in range(y):
		(i, j) = (i, c - j + 1)
	for _ in range(z):
		(i, j) = (c - j + 1, i)
		(r, c) = (c, r)
	print(i, j)


(R, C, x, y, z, p) = map(int, input().split())
x = x % 4
y = y % 2
z = z % 4
for i in range(p):
	(i, j) = map(int, input().split())
	(r, c) = (R, C)
	for _ in range(x):
		(i, j) = (j, r - i + 1)
		(r, c) = (c, r)
	for _ in range(y):
		(i, j) = (i, c - j + 1)
	for _ in range(z):
		(i, j) = (c - j + 1, i)
		(r, c) = (c, r)
	print(i, j)