Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.

ChefX showed ChefY his new pizza slice design. The design is a planar figure that can be drawn in the following way:
Draw a point $\mathsf{A}_0$ and two rays from this point such that the angle between them is $\alpha < \pi/2$.
Mark $N$ points $\mathsf{A}_1, \mathsf{A}_2, \mathsf{A}_3, \ldots, \mathsf{A}_N$ on one ray in this order ($\mathsf{A}_1$ is closest to $\mathsf{A}_0$).
Mark $N$ points $\mathsf{B}_1, \mathsf{B}_2, \mathsf{B}_3, \ldots, \mathsf{B}_N$ on the other ray in this order ($\mathsf{B}_1$ is closest to $\mathsf{A}_0$).
For each valid $i$, draw a line segment between points $\mathsf{A}_i$ and $\mathsf{B}_{i+1}$.
For each valid $i$, draw a line segment between points $\mathsf{B}_i$ and $\mathsf{A}_{i+1}$.
Finally, draw a line segment between points $\mathsf{A}_N$ and $\mathsf{B}_N$.
All these $2N-1$ line segments must have equal lengths, which must also be equal to $|\mathsf{A}_0\mathsf{A}_1| = |\mathsf{A}_0\mathsf{B}_1|$.

The result should look roughly like this:

ChefY wants to show ChefX that he does not know his own design very well. He asks ChefX to compute the angle between three of the marked points in this figure. It is clear that there are only four types of angles:
1. $\angle \mathsf{A}_x\mathsf{A}_y\mathsf{B}_z$
2. $\angle \mathsf{A}_x\mathsf{B}_y\mathsf{A}_z$
3. $\angle \mathsf{A}_x\mathsf{B}_y\mathsf{B}_z$
4. $\angle \mathsf{B}_x\mathsf{A}_y\mathsf{B}_z$

Can you help ChefX find the angle ChefY asks for? It can be proved that under the given constraints, each angle can be written as $\pi \cdot m / n$, where $m$ and $n$ are coprime integers; you should find $m$ and $n$.

------  Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains five space-separated integers $N$, $t$, $x$, $y$ and $z$. Here, $t$ denotes the type of the angle as listed above.

------  Output ------
For each query, print a single line containing two space-separated integers $m$ and $n$.

------  Constraints ------
$1 ≤ T ≤ 10^{5}$
$2 ≤ N ≤ 10^{9}$
$1 ≤ t ≤ 4$
each angle is between three of the points $A_{0}, A_{1}, \ldots, A_{N}, B_{1}, \ldots, B_{N}$
$|x-y| = 1$
$|y-z| = 1$
each angle is non-zero

------  Subtasks ------
Subtask #1 (10 points): $1 ≤ T ≤ 10$

Subtask #2 (90 points): original constraints

----- Sample Input 1 ------ 
1
175456798 1 151163203 151163204 151163205
----- Sample Output 1 ------ 
66583464 116971199
----- explanation 1 ------ 
Example case 1: The type is $1$, so the angle we want is $\mathsf{A}_{151163203}\mathsf{A}_{151163204}\mathsf{B}_{151163205}$. The angle is $(66583464/116971199) \pi \doteq \mathrm{1.7883}$.

Source: codechef | Difficulty: HARD
Tags: Geometry

Mathematics

Ad-hoc


Solutions:
import math
t = int(input())
for i in range(t):
	a = [int(x) for x in input().split()]
	n = a[0]
	op = a[1]
	x = a[2]
	y = a[3]
	z = a[4]
	n1 = n * 2 + 1
	if op == 1:
		if x < y and y < z:
			m = n1 - z
		elif x > y and y > z:
			m = n1 - z
		else:
			m = x
	elif op == 2:
		m = n1 - (z + x)
	elif op == 3:
		if x > y and y > z:
			m = n1 - x
		elif x < y and y < z:
			m = n1 - x
		else:
			m = x
	elif op == 4:
		m = n1 - (z + x)
	gc = math.gcd(m, n1)
	m2 = int(m / gc)
	n2 = int(n1 / gc)
	print(m2, end=' ')
	print(n2)


import math

def solve1(n, x, y, z):
	q = 2 * n + 1
	if (y - x) * (z - y) > 0:
		p = q - z
	else:
		p = x
	return (p, q)

def solve2(n, x, y, z):
	q = 2 * n + 1
	if (y - x) * (z - y) > 0:
		p = q - 2 * y
	else:
		p = 0
	return (p, q)

def solve3(n, x, y, z):
	q = 2 * n + 1
	if y > x and z > y:
		if x == 0:
			p = q
		else:
			p = z + q - 2 * y
	if y > x and z < y:
		p = y - 1
	if y < x and z < y:
		p = q - x
	if y < x and z > y:
		p = z
	return (p, q)

def solve4(n, x, y, z):
	q = 2 * n + 1
	if (y - x) * (z - y) < 0:
		p = 0
	else:
		p = q - x - z
	return (p, q)
T = int(input())
for t in range(0, T):
	(n, t, x, y, z) = map(int, input().split())
	if t == 1:
		(p, q) = solve1(n, x, y, z)
	if t == 2:
		(p, q) = solve2(n, x, y, z)
	if t == 3:
		(p, q) = solve3(n, x, y, z)
	if t == 4:
		(p, q) = solve4(n, x, y, z)
	if int(p):
		print(int(p / math.gcd(p, q)), int(q / math.gcd(p, q)))
	else:
		print(int(p), int(q))


from collections import defaultdict as dd
from collections import Counter as ccd
from itertools import permutations as pp
from itertools import combinations as cc
from random import randint as rd
from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq as hq
from math import gcd
mod = pow(10, 9) + 7

def inp(flag=0):
	if flag == 0:
		return list(map(int, input().strip().split(' ')))
	else:
		return int(input())
t = inp(1)
while t:
	t -= 1
	(n, c, x, y, z) = inp()
	dr = 2 * n + 1
	if c == 1:
		if x < y < z or x > y > z:
			nr = 2 * n + 1 - z
		else:
			nr = x
	elif c == 3:
		if x < y < z or x > y > z:
			nr = 2 * n + 1 - x
		else:
			nr = x
	elif x < y < z or x > y > z:
		nr = 2 * n + 1 - 2 * y
	else:
		nr = 0
	hcf = gcd(nr, dr)
	nr //= hcf
	dr //= hcf
	print(nr, dr)


from math import gcd
t = int(input())
for i in range(t):
	(n, t, x, y, z) = map(int, input().split())
	N = 2 * n + 1
	if t == 1:
		M = 0
		if x > y and y < z:
			M = y + 1
		elif x > y > z:
			M = 2 * n + 2 - y
		elif x < y < z:
			M = 2 * n - y
		elif x < y and y > z:
			M = y - 1
	if t == 3:
		if x > y and y < z:
			M = y + 1
		elif x > y > z:
			M = 2 * n - y
		elif x < y < z:
			M = 2 * n + 2 - y
		elif x < y and y > z:
			M = y - 1
	elif t == 2 or t == 4:
		M = 2 * n - 2 * y + 1
	if gcd(M, N) == 1:
		print(M, N)
	else:
		n1 = N
		m1 = M
		while gcd(M, N) != 1:
			N = N // gcd(m1, n1)
			M = M // gcd(m1, n1)
			n1 = N
			m1 = M
		print(M, N)


import math
for _ in range(int(input())):
	(n, t, x, y, z) = map(int, input().split())
	al = 1 / (2 * n + 1)
	if t == 1:
		if abs(z - x) == 2:
			m = 2 * n + 1 - z
			n = 2 * n + 1
		else:
			m = z
			n = 2 * n + 1
	elif t == 2:
		if abs(z - x) == 2:
			m = 2 * n - 2 * y + 1
			n = 2 * n + 1
		else:
			m = 0
			n = 2 * n + 1
	elif t == 3:
		if abs(z - x) == 2:
			m = 2 * n + 1 - x
			n = 2 * n + 1
		else:
			m = z
			n = 2 * n + 1
	elif abs(z - x) == 2:
		m = 2 * n - 2 * y + 1
		n = 2 * n + 1
	else:
		m = 0
		n = 2 * n + 1
	k = math.gcd(m, n)
	print(m // k, n // k)


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
T = int(input())
while T:
	(N, t, x, y, z) = map(int, input().split())
	a = y - x
	b = z - y
	n = 2 * N + 1
	if t == 2 or t == 4:
		m = n - 2 * y
	elif t == 1:
		if a != b:
			m = z
		else:
			m = n - z
	elif a != b:
		m = x
	else:
		m = n - x
	GCD = gcd(m, n)
	m = m // GCD
	n = n // GCD
	print(m, n)
	T -= 1


def main():
	from sys import stdin, stdout
	from math import ceil, gcd
	inp = stdin.readline
	out = stdout.write
	for i in range(int(inp().strip())):
		(n, t, x, y, z) = map(int, inp().strip().split())
		d = 2 * n + 1
		if t == 2 or t == 4:
			nm = 2 * n + 1 - 2 * y
		elif t == 1:
			if x == z:
				nm = x
			else:
				nm = 2 * n + 1 - z
		elif x == z:
			nm = x
		else:
			nm = 2 * n + 1 - x
		g = gcd(nm, d)
		print(nm // g, d // g)
main()


def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
T = int(input())
for i in range(T):
	(N, t, x, y, z) = input().split()
	if t == '1':
		if x != z:
			a = 2 * int(N) + 1 - int(z)
			b = 2 * int(N) + 1
			c = gcd(a, b)
			print(str(int(a / c)) + ' ' + str(int(b / c)))
		else:
			a = int(x)
			b = 2 * int(N) + 1
			c = gcd(a, b)
			print(str(int(a / c)) + ' ' + str(int(b / c)))
	elif t == '2' or t == '4':
		a = 2 * int(N) + 1 - 2 * int(y)
		b = 2 * int(N) + 1
		c = gcd(a, b)
		print(str(int(a / c)) + ' ' + str(int(b / c)))
	elif t == '3':
		if x != z:
			a = 2 * int(N) + 1 - int(x)
			b = 2 * int(N) + 1
			c = gcd(a, b)
			print(str(int(a / c)) + ' ' + str(int(b / c)))
		else:
			a = int(x)
			b = 2 * int(N) + 1
			c = gcd(a, b)
			print(str(int(a / c)) + ' ' + str(int(b / c)))


def ii():
	return int(input())

def spi():
	return input().split()

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
for spartans in range(int(input())):
	n = list(mi())
	creed = 2 * n[0] + 1
	assasin = 1
	if n[1] == 3:
		if n[2] != n[4]:
			assasin = creed - n[2]
		else:
			assasin = n[2]
		print(assasin // gcd(assasin, creed), creed // gcd(assasin, creed))
		continue
	if n[1] == 1:
		if n[2] == n[4]:
			assasin = n[4]
		else:
			assasin = creed - n[4]
		print(assasin // gcd(assasin, creed), creed // gcd(assasin, creed))
		continue
	else:
		assasin = creed - 2 * n[3]
		print(assasin // gcd(assasin, creed), creed // gcd(assasin, creed))
		continue


from math import gcd

def disp(x):
	one = x
	two = 2 * n + 1
	b = gcd(one, two)
	one //= b
	two //= b
	print(one, two)
p = int(input())
for z in range(p):
	(n, t, x, y, z) = [int(x) for x in input().split()]
	den = 2 * n + 1
	if t == 1:
		if abs(x - z) == 0:
			disp(x)
		elif x - z == 1:
			one = abs(den - z)
			disp(one)
		else:
			one = den - z
			disp(one)
	elif t == 3:
		if abs(x - z) == 0:
			one = x
			disp(one)
		elif x - z == 1:
			one = abs(den - x)
			disp(one)
		else:
			one = abs(den - x)
			disp(one)
	elif t == 2:
		if abs(x - z) == 0:
			print(1, 1)
		else:
			one = abs(den - 2 * y)
			disp(one)
	elif t == 4:
		if abs(x - z) == 0:
			print(1, 1)
		elif abs(x - z):
			one = abs(den - 2 * y)
			disp(one)


def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
t = int(input())
for i in range(0, t):
	(n, t, x, y, z) = map(int, input().split())
	if t == 1:
		if x == z:
			f = gcd(x, 2 * n + 1)
			print(x // f, ' ', (2 * n + 1) // f)
		else:
			f = gcd(2 * n + 1 - z, 2 * n + 1)
			print((2 * n + 1 - z) // f, ' ', (2 * n + 1) // f)
	if t == 2:
		f = gcd(2 * n + 1 - 2 * y, 2 * n + 1)
		print((2 * n + 1 - 2 * y) // f, ' ', (2 * n + 1) // f)
	if t == 3:
		if x == z:
			f = gcd(x, 2 * n + 1)
			print(x // f, ' ', (2 * n + 1) // f)
		else:
			f = gcd(2 * n + 1 - x, 2 * n + 1)
			print((2 * n + 1 - x) // f, ' ', (2 * n + 1) // f)
	if t == 4:
		f = gcd(2 * n + 1 - 2 * y, 2 * n + 1)
		print((2 * n + 1 - 2 * y) // f, ' ', (2 * n + 1) // f)


import sys
sys.setrecursionlimit(10 ** 9)

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def frac_reduce_print(a, b):
	d = gcd(a, b)
	(a, b) = (a // d, b // d)
	print(a, b)
for _ in range(int(input())):
	(N, t, x, y, z) = [int(i) for i in input().split()]
	cons = 2 * N + 1
	if x * y * z == 0:
		if y == 0:
			frac_reduce_print(1, cons)
		elif t == 3 and x == 0:
			frac_reduce_print(1, 1)
		else:
			frac_reduce_print(cons - 2, cons)
	elif t == 3 or t == 1:
		(x, z) = (z, x) if t == 3 else (x, z)
		if y > x and y < z:
			frac_reduce_print(cons - 1 - y, cons)
		elif x < y and y > z:
			frac_reduce_print(y - 1, cons)
		elif x > y and y < z:
			frac_reduce_print(y + 1, cons)
		else:
			frac_reduce_print(cons + 1 - y, cons)
	else:
		frac_reduce_print(cons - 2 * y, cons)


def gcd(x, y):
	if y > x:
		temp = x
		x = y
		y = temp
	if y == 0:
		return x
	return gcd(y, x % y)
loop = int(input())
for i in range(loop):
	(n, t, x, y, z) = map(int, input().split())
	dtr = 2 * n + 1
	if t == 1:
		if (x - y) * (y - z) == 1:
			ntr = dtr - z
		elif x - y == -1 and y - z == 1:
			ntr = x
		elif x - y == 1 and y - z == -1:
			if x == n:
				ntr = dtr - n - 1
			else:
				ntr = x
	elif t == 2 or t == 4:
		y *= 2
		ntr = dtr - y
	elif t == 3:
		if (x - y) * (y - z) == 1:
			ntr = dtr - x
		elif (x - y) * (y - z) == -1:
			ntr = x
	cd = gcd(ntr, dtr)
	print(ntr // cd, ' ', dtr // cd)


def gcd(p, q):
	if p == 0:
		return q
	else:
		return gcd(q % p, p)
num = int(input())
for k in range(num):
	l = list(map(int, input().split()))
	(n, typ, x, y, z) = l
	dnr = 2 * n + 1
	if typ == 1:
		if x != z:
			nmr = dnr - z
		else:
			nmr = z
	elif typ == 2 or typ == 4:
		nmr = dnr - 2 * y
	elif typ == 3:
		if x != z:
			nmr = dnr - x
		else:
			nmr = x
	print(nmr // gcd(nmr, dnr), dnr // gcd(nmr, dnr))


import math
t1 = int(input())
while t1 != 0:
	(n, t, x, y, z) = map(int, input().split())
	a = 0
	b = 0
	n1 = 0
	m = 0
	if t == 1:
		if x < y and x != z:
			a = 2 * n + 1 - z
			b = 2 * n + 1
			m = a / math.gcd(a, b)
			n1 = b / math.gcd(a, b)
		if x < y and x == z:
			a = x
			b = 2 * n + 1
			m = a / math.gcd(a, b)
			n1 = b / math.gcd(a, b)
		elif x > y and x != z:
			a = 2 * n + 1 - z
			b = 2 * n + 1
			m = a / math.gcd(a, b)
			n1 = b / math.gcd(a, b)
		elif x == z:
			a = x
			b = 2 * n + 1
			m = a / math.gcd(a, b)
			n1 = b / math.gcd(a, b)
	elif t == 2:
		a = 2 * n - 2 * y + 1
		b = 2 * n + 1
		m = a / math.gcd(a, b)
		n1 = b / math.gcd(a, b)
	elif t == 3:
		if x < y and x != z:
			a = 2 * n + 1 - x
			b = 2 * n + 1
			m = a / math.gcd(a, b)
			n1 = b / math.gcd(a, b)
		elif x < y and x == z:
			a = x
			b = 2 * n + 1
			m = a / math.gcd(a, b)
			n1 = b / math.gcd(a, b)
		elif x > y and x != z:
			a = 2 * n + 1 - x
			b = 2 * n + 1
			m = a / math.gcd(a, b)
			n1 = b / math.gcd(a, b)
		elif x == z:
			a = x
			b = 2 * n + 1
			m = a / math.gcd(a, b)
			n1 = b / math.gcd(a, b)
	elif t == 4:
		a = 2 * n - 2 * y + 1
		b = 2 * n + 1
		m = a / math.gcd(a, b)
		n1 = b / math.gcd(a, b)
	print(int(m), int(n1))
	t1 = t1 - 1


from math import *

def theta1(y, lower):
	print((y + 1) // gcd(y + 1, lower), lower // gcd(y + 1, lower))

def theta2(y, lower, n):
	print((2 * n - y + 2) // gcd(2 * n - y + 2, lower), lower // gcd(2 * n - y + 2, lower))

def theta3(y, lower):
	print((y - 1) // gcd(y - 1, lower), lower // gcd(y - 1, lower))

def theta4(y, lower, n):
	print((2 * n - y) // gcd(2 * n - y, lower), lower // gcd(2 * n - y, lower))

def theta5(y, lower, n):
	print((2 * n + 1 - 2 * y) // gcd(2 * n + 1 - 2 * y, lower), lower // gcd(2 * n + 1 - 2 * y, lower))

def theta6(y, lower):
	print(0, lower)

def answer_pizza_question(n, t, x, y, z):
	lower = 2 * n + 1
	if t == 2:
		if z == x:
			theta6(y, lower)
		else:
			theta5(y, lower, n)
	elif t == 3:
		if x > y:
			if z > y:
				theta1(y, lower)
			else:
				theta4(y, lower, n)
		elif x < y:
			if z > y:
				theta2(y, lower, n)
			else:
				theta3(y, lower)
	elif t == 4:
		if x == z:
			theta6(y, lower)
		else:
			theta5(y, lower, n)
	elif t == 1:
		if x > y:
			if z > y:
				theta1(y, lower)
			else:
				theta2(y, lower, n)
		elif z < y:
			theta3(y, lower)
		elif z > y:
			theta4(y, lower, n)
for _ in range(int(input())):
	(n, t, x, y, z) = map(int, input().split())
	answer_pizza_question(n, t, x, y, z)


def gcd(a, b):
	while a % b > 0:
		t = a % b
		a = b
		b = t
	return b

def foo1(a, x, y, z):
	if x > y:
		if y > z:
			return a - z
		else:
			return z
	elif y > z:
		return z
	else:
		return a - z

def foo2(a, x, y, z):
	return a - 2 * y

def foo3(a, x, y, z):
	if x > y:
		if y > z:
			return a - x
		else:
			return x
	elif y > z:
		return x
	else:
		return a - x

def foo4(a, x, y, z):
	return a - 2 * y

def main():
	t = int(input())
	for i in range(t):
		q = [int(q) for q in input().split()]
		a = 2 * q[0] + 1
		b = a
		if q[1] == 1:
			b = foo1(a, q[2], q[3], q[4])
		elif q[1] == 2:
			b = foo2(a, q[2], q[3], q[4])
		elif q[1] == 3:
			b = foo3(a, q[2], q[3], q[4])
		elif q[1] == 4:
			b = foo4(a, q[2], q[3], q[4])
		gcda = gcd(a, b)
		print(f'{b // gcda} {a // gcda}')
main()


import math
test = int(input())
for _ in range(test):
	(n, type, x, y, z) = map(int, input().split())
	alpha = [1, 2 * n + 1]
	ans = [1, 2 * n + 1]
	if y == z:
		print(1, 2 * n + 1)
	else:
		if type == 1:
			if x < y and y < z:
				ans[0] = alpha[1] - z * alpha[0]
			elif x == z and y > x:
				ans[0] = x * alpha[0]
			elif x > y and y > z:
				ans[0] = alpha[1] - z * alpha[0]
			else:
				ans[0] = x * alpha[0]
		elif type == 2 or type == 4:
			ans[0] = alpha[1] - y * 2 * alpha[0]
		elif type == 3:
			if x < y and y < z:
				ans[0] = alpha[1] - x * alpha[0]
			elif x == z and y > x:
				ans[0] = x * alpha[0]
			elif x > y and y > z:
				ans[0] = alpha[1] - x * alpha[0]
			else:
				ans[0] = x * alpha[0]
		temp = math.gcd(ans[0], ans[1])
		ans[0] //= temp
		ans[1] //= temp
		print(ans[0], ans[1])


from math import gcd
for _ in range(int(input())):
	(n, t, x, y, z) = [int(i) for i in input().split()]
	if t == 1:
		if x < y and y <= z:
			g = gcd(2 * n - y, 2 * n + 1)
			print((2 * n - y) // g, (2 * n + 1) // g)
		elif x < y and y > z:
			g = gcd(y - 1, 2 * n + 1)
			print((y - 1) // g, (2 * n + 1) // g)
		elif x >= y and y <= z:
			g = gcd(y + 1, 2 * n + 1)
			print((y + 1) // g, (2 * n + 1) // g)
		elif x >= y and y > z:
			g = gcd(2 * n - y + 2, 2 * n + 1)
			print((2 * n - y + 2) // g, (2 * n + 1) // g)
	elif t == 2:
		if x == z:
			print(0, 2 * n + 1)
		else:
			g = gcd(2 * n - 2 * y + 1, 2 * n + 1)
			print((2 * n - 2 * y + 1) // g, (2 * n + 1) // g)
	elif t == 3:
		if x < y and y <= z:
			g = gcd(2 * n - y + 2, 2 * n + 1)
			print((2 * n - y + 2) // g, (2 * n + 1) // g)
		elif x < y and y > z:
			g = gcd(y - 1, 2 * n + 1)
			print((y - 1) // g, (2 * n + 1) // g)
		elif x >= y and y <= z:
			g = gcd(y + 1, 2 * n + 1)
			print((y + 1) // g, (2 * n + 1) // g)
		elif x >= y and y > z:
			g = gcd(2 * n - y, 2 * n + 1)
			print((2 * n - y) // g, (2 * n + 1) // g)
	elif x == z:
		print(0, 2 * n + 1)
	else:
		g = gcd(2 * n - 2 * y + 1, 2 * n + 1)
		print((2 * n - 2 * y + 1) // g, (2 * n + 1) // g)


import math
T = int(input())
for xx in range(T):
	(n, t, x, y, z) = map(int, input().split())
	(a, b) = (0, 0)
	if t == 1:
		if x < y:
			if y < z:
				(a, b) = (2 * n + 1 - z, 2 * n + 1)
			if y > z:
				(a, b) = (z, 2 * n + 1)
		if x > y:
			if y > z:
				(a, b) = (2 * n + 1 - z, 2 * n + 1)
			if y < z:
				(a, b) = (z, 2 * n + 1)
	if t == 2 or t == 4:
		(a, b) = (2 * n + 1 - 2 * y, 2 * n + 1)
	if t == 3:
		if x < y:
			if y < z:
				(a, b) = (2 * n + 1 - x, 2 * n + 1)
			if y > z:
				(a, b) = (x, 2 * n + 1)
		if x > y:
			if y < z:
				(a, b) = (x, 2 * n + 1)
			if y > z:
				(a, b) = (2 * n + 1 - x, 2 * n + 1)
	g = math.gcd(a, b)
	print(a // g, b // g)


import math
t = int(input())
for i in range(t):
	(N, t, x, y, z) = map(int, input().split())
	m = 0
	n = 2 * N + 1
	if t == 1:
		if x == z:
			m = x
		else:
			m = 2 * N + 1 - z
	elif t == 2:
		m = 2 * N + 1 - 2 * y
	elif t == 3:
		if x == z:
			m = x
		else:
			m = 2 * N + 1 - x
	elif t == 4:
		m = 2 * N + 1 - 2 * y
	g_cd = math.gcd(m, n)
	m = m / g_cd
	n = n / g_cd
	print(int(m), int(n))


from math import gcd
from math import pi
z = int(input())
while z:
	(n, t, x, y, z1) = map(int, input().split())
	a = 2 * n + 1
	if t == 1:
		if x < y:
			if x == z1:
				b = x
			else:
				b = a - 1 - y
		elif x == z1:
			b = y + 1
		else:
			b = a - y + 1
	elif t == 2 or t == 4:
		b = a - 2 * y
	elif t == 3:
		if z1 < y:
			if x == z1:
				b = x
			else:
				b = a - 1 - y
		elif x == z1:
			b = y + 1
		else:
			b = a - y + 1
	c = gcd(a, b)
	print(b // c, a // c)
	z = z - 1


from math import gcd

def pri(x, y):
	g = gcd(x, y)
	print(x // g, y // g)
tc = int(input())
for _ in range(tc):
	(n, t, x, y, z) = [int(i) for i in input().split(' ')]
	de = 2 * n + 1
	if t == 1:
		if y == 0:
			pri(1, de)
		elif x < y:
			if x == z:
				pri(y - 1, de)
			elif z == y + 1:
				pri(de - y - 1, de)
		elif x == z:
			pri(y + 1, de)
		elif z == y - 1:
			pri(de - y + 1, de)
	elif t == 2 or t == 4:
		pri(de - 2 * y, de)
	elif t == 3:
		(x, y, z) = (z, y, x)
		if x < y:
			if x == z:
				pri(y - 1, de)
			elif z == y + 1:
				pri(de - y - 1, de)
		elif x == z:
			pri(y + 1, de)
		elif z == y - 1:
			pri(de - y + 1, de)


from math import gcd
for i in range(int(input())):
	(n, t, x, y, z) = map(int, input().split())
	N = n * 2 + 1
	if x + 1 == y and y + 1 == z:
		T = [0, 2 + x, (1 + x) * 2, x, (1 + x) * 2]
	elif x == y + 1 and y == z + 1:
		T = [0, x - 2, 2 * (x - 1), x, 2 * (x - 1)]
	elif x == z and y - 1 == x:
		T = [0, 2 * n + 1 - x, 0, 2 * n + 1 - x, 0]
	else:
		T = [0, 2 * n + 1 - x, 0, 2 * n + 1 - x, 0]
	m = N - T[t]
	g = gcd(N, m)
	print(m // g, N // g)


from fractions import gcd
for _ in range(int(input())):
	(N, t, x, y, z) = input().split(' ')
	(N, t, x, y, z) = (int(N), int(t), int(x), int(y), int(z))
	b = 2 * N + 1
	if t == 1:
		if x < y:
			if y > z:
				a = x
			else:
				a = b - (y + 1)
		elif y > z:
			a = b - z
		else:
			a = y + 1
	elif t == 2:
		a = b - 2 * (min(x, y) + 1)
	elif t == 3:
		if x == 0:
			a = b
		if y < z:
			if x > y:
				a = y + 1
			else:
				a = b - (y - 1)
		elif x > y:
			a = b - (y + 1)
		else:
			a = z
	elif t == 4:
		a = b - 2 * (min(x, z) + 1)
	r = gcd(a, b)
	while r > 1:
		r = gcd(a, b)
		a = a // r
		b = b // r
	print(str(a) + ' ' + str(b))


import math
T = int(input())
for ww in range(T):
	(n, t, x, y, z) = (int(x) for x in input().split())
	M = 0
	N = 2 * n + 1
	if t == 1:
		if x == z:
			M = x
		else:
			M = N - z
	elif t == 2:
		if x == z:
			M = 0
		else:
			M = N - z - x
	elif t == 3:
		if x == z:
			M = x
		else:
			M = N - x
	elif x == z:
		M = 0
	else:
		M = N - z - x
	gcd = math.gcd(M, N)
	print(M // gcd, end=' ')
	print(N // gcd)


from math import gcd
test1 = input()
test = int(test1)
list1 = []
for i in range(test):
	list1.append(list(map(int, input().split())))
for i in range(test):
	n = list1[i][0]
	t = list1[i][1]
	x = list1[i][2]
	y = list1[i][3]
	z = list1[i][4]
	if t == 1 and x > y and (y > z):
		m = int((2 * n + 2 - y) / gcd(2 * n + 2 - y, 2 * n + 1))
		n = int((2 * n + 1) / gcd(2 * n + 2 - y, 2 * n + 1))
		print(m, n)
	elif t == 1 and x > y and (y < z):
		m = int((y + 1) / gcd(y + 1, 2 * n + 1))
		n = int((2 * n + 1) / gcd(y + 1, 2 * n + 1))
		print(m, n)
	elif t == 1 and x < y and (y < z):
		m = int((2 * n - y) / gcd(2 * n - y, 2 * n + 1))
		n = int((2 * n + 1) / gcd(2 * n - y, 2 * n + 1))
		print(m, n)
	elif t == 1 and x < y and (y > z):
		m = int((y - 1) / gcd(y - 1, 2 * n + 1))
		n = int((2 * n + 1) / gcd(y - 1, 2 * n + 1))
		print(m, n)
	elif t == 3 and x > y and (y > z):
		m = int((2 * n - y) / gcd(2 * n - y, 2 * n + 1))
		n = int((2 * n + 1) / gcd(2 * n - y, 2 * n + 1))
		print(m, n)
	elif t == 3 and x > y and (y < z):
		m = int((y + 1) / gcd(y + 1, 2 * n + 1))
		n = int((2 * n + 1) / gcd(y + 1, 2 * n + 1))
		print(m, n)
	elif t == 3 and x < y and (y < z):
		m = int((2 * n + 2 - y) / gcd(2 * n + 2 - y, 2 * n + 1))
		n = int((2 * n + 1) / gcd(2 * n + 2 - y, 2 * n + 1))
		print(m, n)
	elif t == 3 and x < y and (y > z):
		m = int((y - 1) / gcd(y - 1, 2 * n + 1))
		n = int((2 * n + 1) / gcd(y - 1, 2 * n + 1))
		print(m, n)
	elif t == 4 or t == 2:
		m = int((2 * n + 1 - 2 * y) / gcd(2 * n + 1 - 2 * y, 2 * n + 1))
		n = int((2 * n + 1) / gcd(2 * n + 1 - 2 * y, 2 * n + 1))
		print(m, n)
	else:
		pass


from fractions import Fraction
from math import gcd
t = int(input())
for i in range(t):
	(n, t, x, y, z) = map(int, input().split())
	ang = 2 * n + 1
	if t == 1:
		if x == z:
			ans = (z, ang)
		else:
			ans = (ang - z, ang)
	elif t == 2:
		ans = (ang - 2 * y, ang)
	elif t == 3:
		if x == z:
			ans = (x, ang)
		else:
			ans = (ang - x, ang)
	elif t == 4:
		ans = (ang - 2 * y, ang)
	pk = gcd(ans[0], ans[1])
	print(ans[0] // pk, ans[1] // pk)


import math

def gcd(e, r):
	if e > r:
		(e, r) = (r, e)
	while r % e != 0:
		(r, e) = (e, r % e)
	return e

def w(f, p):
	yt = gcd(f, p)
	f = f // yt
	p = p // yt
	print(f, p)
qw = int(input())
for wq in range(qw):
	(n, t, a, b, c) = map(int, input().split())
	d = 2 * n + 1
	nj = 0
	if c < b:
		nj = 1
	if (t == 1 or t == 3) and a == c:
		w(a, d)
	elif a == 0 and b == 1 and (c == 2):
		if t != 3:
			w(d - 2, d)
		else:
			print(1, 1)
	elif t == 2 or t == 4:
		p = d - 2
		f = p - 2 * (b - 1)
		w(f, d)
	elif t == 1:
		if nj == 1:
			f = d - (b - 1)
		else:
			p = d - 2
			f = p - (b - 1)
		w(f, d)
	elif t == 3:
		if nj == 0:
			f = d - (b - 1)
		else:
			f = d - (b + 1)
		w(f, d)


from fractions import Fraction

def zeroPad(n, zeros, left=True):
	for i in range(zeros):
		if left:
			n = '0' + n
		else:
			n = n + '0'
	return n

def func(x, y):
	x = str(x)
	y = str(y)
	if len(x) == 1 and len(y) == 1:
		return int(x) * int(y)
	if len(x) < len(y):
		x = zeroPad(x, len(y) - len(x))
	elif len(y) < len(x):
		y = zeroPad(y, len(x) - len(y))
	n = len(x)
	j = n // 2
	if n % 2 != 0:
		j += 1
	BZeroPadding = n - j
	AZeroPadding = BZeroPadding * 2
	a = int(x[:j])
	b = int(x[j:])
	c = int(y[:j])
	d = int(y[j:])
	ac = func(a, c)
	bd = func(b, d)
	k = func(a + b, c + d)
	A = int(zeroPad(str(ac), AZeroPadding, False))
	B = int(zeroPad(str(k - ac - bd), BZeroPadding, False))
	return A + B + bd
for _ in range(int(input())):
	(a, b, c, d, e) = map(int, input().split())
	y = func(2, 1)
	if c > d > e and c != e:
		if b == y or b == y + 2:
			m = a * 2 + 1 - d * 2
			n = a * 2 + 1
		elif b == y - 1:
			m = a * 2 + 2 - d
			n = a * 2 + 1
		elif b == y + 1:
			m = a * 2 - d
			n = a * 2 + 1
	elif c < d < e and c != e:
		if b == y - 1:
			m = a * 2 - d
			n = a * 2 + 1
		elif b == y or b == y + 2:
			m = a * 2 + 1 - d * 2
			n = a * 2 + 1
		elif b == y + 1:
			m = a * 2 + y - d
			n = a * 2 + 1
	elif c == e:
		if b == y - 1 or b == y + 1:
			if c < d:
				(m, n) = (d - 1, a * y + 1)
			else:
				(m, n) = (d + 1, a * y + 1)
		elif b == y or b == y + 2:
			(n, m) = (y - 1, y - 2)
	print(Fraction(m, n).numerator, Fraction(m, n).denominator)


from math import gcd
te = int(input())
for j in range(te):
	(n, t, x, y, z) = map(int, input().split())
	alpha1 = 90 * 2
	alpha2 = (n - 1 + 2) * 2 - 1
	if t == 1:
		if y > x and z > y:
			r1 = 180 * alpha2 + ((n - z) * 2 + 1) * alpha1
			w = 180 * 2 * alpha2
		if x > y and y > z:
			r1 = 90 * alpha2 * 2 + alpha1 + 2 * (n - x + 2) * alpha1
			w = 180 * 2 * alpha2
		if x > y and z > y:
			r1 = 90 * alpha2 * 2 + alpha1 - 2 * (n - y) * alpha1
			w = 180 * 2 * alpha2
		if y > x and y > z:
			r1 = 90 * alpha2 * 2 - alpha1 - 2 * (n - x) * alpha1
			w = 180 * 2 * alpha2
	elif t == 2 or t == 4:
		if y > x and z > y:
			r1 = (2 * (n - y) + 1) * alpha1
			w = alpha2 * 180
		if x > y and y > z:
			r1 = (2 * (n - y) + 1) * alpha1
			w = alpha2 * 180
	else:
		if y > x and z > y:
			r1 = 90 * alpha2 * 2 + alpha1 + 2 * (n - x) * alpha1
			w = 180 * 2 * alpha2
		if x > y and y > z:
			r1 = 90 * alpha2 * 2 - alpha1 + 2 * (n - y) * alpha1
			w = 180 * 2 * alpha2
		if x > y and z > y:
			r1 = 90 * alpha2 * 2 + alpha1 - 2 * (n - y) * alpha1
			w = 180 * 2 * alpha2
		if y > x and y > z:
			r1 = 90 * alpha2 * 2 - alpha1 - 2 * (n - x) * alpha1
			w = 180 * 2 * alpha2
	f = gcd(r1, w)
	m = r1 // f
	n = w // f
	print(m, end=' ')
	print(n)


import math

def solve():
	(N1, t1, x1, y1, z1) = input().split()
	N = int(N1)
	t = int(t1)
	x = int(x1)
	y = int(y1)
	z = int(z1)
	s = 2 * (N + 1)
	s = s - 1
	if x == z and (t == 1 or t == 3):
		print(int(x / math.gcd(x, s)), int(s / math.gcd(x, s)))
	elif x - z == 2 or x - z == -2:
		if t == 1 and (x - z == 2 or x - z == -2):
			print(int((s - z) / math.gcd(s - z, s)), int(s / math.gcd(s - z, s)))
		elif t == 3 and (x - z == 2 or x - z == -2):
			print(int((s - x) / math.gcd(s - x, s)), int(s / math.gcd(s - x, s)))
		else:
			mu = x + z
			print(int((s - mu) / math.gcd(s - mu, s)), int(s / math.gcd(s - mu, s)))
me = int(input())
for you in range(me):
	solve()


import math
for _ in range(int(input())):
	(N, t, x, y, z) = list(map(int, input().split()))
	d = 2 * N + 1
	if t == 1:
		if z > y and y > x:
			n = d - z
		elif x == z:
			n = x
		elif x > y and y > z:
			n = d - z
	elif t == 2:
		n = d - 2 * y
	elif t == 3:
		if z > y and y > x:
			n = d - x
		elif x == z:
			n = x
		elif x > y and y > z:
			n = d - x
	elif t == 4:
		n = d - 2 * y
	x = math.gcd(n, d)
	print(n // x, d // x)


import math
for _ in range(int(input())):
	(n, t, x, y, z) = [int(x) for x in input().rstrip().split(' ')]
	io = 2 * n + 1
	num = x
	den = io
	if t == 1:
		if x != z:
			num = io - z
	if t == 2 or t == 4:
		num = io - y - y
	if t == 3:
		if x != z:
			num = io - x
	hcf = math.gcd(num, den)
	num = num // hcf
	den = den // hcf
	print(num, end=' ')
	print(den)


import math
T = int(input())
for _ in range(T):
	(N, t, x, y, z) = map(int, input().split())
	marks = []
	n = 2 * N + 1
	if t % 2 == 1:
		if t == 3:
			(x, z) = (z, x)
		if x == y + 1 and z == y + 1:
			m = y + 1
		elif x == y - 1 and z == y + 1:
			m = 2 * N - y
		elif x == y - 1 and z == y - 1:
			m = y - 1
		elif x == y + 1 and z == y - 1:
			m = 2 * N - y + 2
	elif x == z:
		m = 0
		n = 1
	else:
		m = 2 * N - 2 * y + 1
	if m != 0:
		gcd = math.gcd(m, n)
	else:
		gcd = 1
	print(int(m / gcd), int(n / gcd))


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
for i in range(int(input())):
	(n, t, x, y, z) = [int(y) for y in input().split()]
	al = 2 * n + 1
	m = 0
	gc = 0
	if t == 1:
		if x < y and y > z:
			m = x
		elif x > y and y < z:
			m = x
		elif x < y and y < z:
			m = al - z
		elif x > y and y > z:
			m = al - z
	elif t == 2 or t == 4:
		m = al - 2 * y
	elif t == 3:
		if x < y and y > z:
			m = x
		elif x > y and y < z:
			m = x
		elif x > y and y > z:
			m = al - x
		elif x < y and y < z:
			m = al - x
	gc = gcd(m, al)
	al = al // gc
	m = m // gc
	print(m, end=' ')
	print(al)


T = int(input())
for i in range(T):
	(N, t, x, y, z) = map(int, input().split())

	def gcd(a, b):
		if b == 0:
			return a
		else:
			return gcd(b, a % b)
	if t == 1:
		if x == z:
			print(int(x / gcd(x, 2 * N + 1)), int((2 * N + 1) / gcd(x, 2 * N + 1)))
		else:
			print(int((2 * N + 1 + x - 2 * y) / gcd(2 * N + 1 + x - 2 * y, 2 * N + 1)), int((2 * N + 1) / gcd(2 * N + 1 + x - 2 * y, 2 * N + 1)))
	if t == 2 or t == 4:
		if y == z or (x == y and y == N):
			print(1, int(2 * N + 1))
		else:
			print(int((2 * N + 1 - 2 * y) / gcd(2 * N + 1 - 2 * y, 2 * N + 1)), int((2 * N + 1) / gcd(2 * N + 1 - 2 * y, 2 * N + 1)))
	if t == 3:
		if x == z:
			print(int(x / gcd(x, 2 * N + 1)), int((2 * N + 1) / gcd(x, 2 * N + 1)))
		elif x == y and x == N:
			print(int(x / gcd(x, 2 * N + 1)), int((2 * N + 1) / gcd(x, 2 * N + 1)))
		else:
			print(int((2 * N + 1 + z - 2 * y) / gcd(2 * N + 1 + z - 2 * y, 2 * N + 1)), int((2 * N + 1) / gcd(2 * N + 1 + z - 2 * y, 2 * N + 1)))
quit()


def determine_type(N, t, x, y, z):
	full = -1
	left = 0
	mid = 1
	right = 2
	left_mid = 3
	right_mid = 4
	if y == 0:
		assert t == 1
		return right
	elif y == 1:
		if x == z == 2:
			return right
		elif x == 0 and z == 2:
			if t == 3:
				return full
			else:
				assert t != 4
				return mid
		elif x == 2 and z == 0:
			assert t == 2
			return mid
	elif y == N:
		assert t == 1 or t == 3
		return left
	else:
		assert 1 < y < N
		if x == z:
			assert t == 1 or t == 3
			if x < y:
				return left
			else:
				assert x > y
				return right
		elif t == 2 or t == 4:
			return mid
		elif x < z:
			if t == 1:
				return left_mid
			else:
				assert t == 3
				return right_mid
		else:
			assert x > z
			if t == 1:
				return right_mid
			else:
				assert t == 3
				return left_mid

def get_frac(N, t, x, y, z):
	theta = 2 * N + 1
	angle_type = determine_type(N, t, x, y, z)
	if angle_type == -1:
		return (1, 1)
	if angle_type == 0:
		return (y - 1, theta)
	if angle_type == 1:
		return (2 * N + 1 - 2 * y, theta)
	if angle_type == 2:
		return (y + 1, theta)
	if angle_type == 3:
		return (2 * N - y, theta)
	if angle_type == 4:
		return (2 * N - y + 2, theta)

def gcd(x, y):
	while y != 0:
		t = y
		y = x % y
		x = t
	return x
T = int(input())
for _ in range(T):
	(N, t, x, y, z) = map(int, input().strip().split())
	(num, dom) = get_frac(N, t, x, y, z)
	fac = gcd(num, dom)
	num = num // fac
	dom = dom // fac
	print('{} {}'.format(num, dom))


import math as m
for _ in range(int(input())):
	(n, t, x, y, z) = map(int, input().split())
	if t % 2 == 1:
		if x == z:
			a = z
		elif t == 1:
			a = 2 * n - z + 1
		else:
			a = 2 * n - x + 1
	else:
		a = 2 * n - 2 * y + 1
	g = m.gcd(a, 2 * n + 1)
	a = int(a / g)
	b = int((2 * n + 1) / g)
	print(a, b)


def mathgcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
T = int(input())
while T:
	ip = input().split()
	(n, t, x, y, z) = (int(ip[0]), int(ip[1]), int(ip[2]), int(ip[3]), int(ip[4]))
	if t == 1:
		if x != z:
			m = int(2 * n + 1 - z)
			n = int(2 * n + 1)
		else:
			m = int(z)
			n = int(2 * n + 1)
		g = int(mathgcd(m, n))
		print(int(m / g), int(n / g))
	elif t == 2 or t == 4:
		m = int(2 * n + 1 - 2 * y)
		n = int(2 * n + 1)
		g = int(mathgcd(m, n))
		print(int(m / g), int(n / g))
	elif t == 3:
		if x != z:
			m = int(2 * n + 1 - x)
		else:
			m = int(x)
		n = int(2 * n + 1)
		g = int(mathgcd(m, n))
		print(int(m / g), int(n / g))
	T -= 1


import math
tt = int(input())
for i in range(tt):
	(n, t, x, y, z) = map(int, input().split())
	if t == 1:
		if x == z:
			p = x
			q = 2 * n + 1
		else:
			p = 2 * n - z + 1
			q = 2 * n + 1
	elif t == 2:
		if x == z:
			print(2, 1)
			continue
		else:
			p = 2 * (n - y) + 1
			q = 2 * n + 1
	elif t == 3:
		if x == z:
			p = x
			q = 2 * n + 1
		else:
			p = 2 * n - x + 1
			q = 2 * n + 1
	elif x == z:
		print(2, 1)
		continue
	else:
		p = 2 * (n - y) + 1
		q = 2 * n + 1
	print(p // math.gcd(p, q), q // math.gcd(p, q))


def GCD(a, b):
	if a == 0:
		return b
	return GCD(b % a, a)
for test in range(int(input())):
	(N, t, x, y, z) = [int(x) for x in input().split()]
	denom = 1 + 2 * N
	if t == 2 or t == 4:
		counter = denom - 2 * y
	elif t == 3:
		if x < y < z:
			counter = denom - y + 1
		elif x == z:
			if y > x:
				counter = y - 1
			else:
				counter = y + 1
		elif z < x:
			counter = denom - 1 - y
	elif t == 1:
		if x < y < z:
			counter = denom - y - 1
		elif x == z:
			if y > x:
				counter = y - 1
			else:
				counter = y + 1
		else:
			counter = denom + 1 - y
	factor = GCD(counter, denom)
	print('{} {}'.format(counter // factor, denom // factor))


T = int(input())
from math import gcd
while T:
	T -= 1
	(n, t, x, y, z) = map(int, input().strip().split())
	if t == 1:
		if x != z:
			a = 2 * n - z + 1
			b = n * 2 + 1
			g = gcd(a, b)
			print(a // g, end=' ')
			print(b // g)
		else:
			a = x
			b = 2 * n + 1
			g = gcd(a, b)
			print(a // g, end=' ')
			print(b // g)
	elif t == 2 or t == 4:
		if x != z:
			a = 2 * n - 2 * y + 1
			b = 2 * n + 1
			g = gcd(a, b)
			print(a // g, end=' ')
			print(b // g)
		else:
			a = 0
			b = 1
			print(a, end=' ')
			print(b)
	elif t == 3:
		if x != z:
			a = 2 * n + 1 - x
			b = 2 * n + 1
			g = gcd(a, b)
			print(a // g, end=' ')
			print(b // g)
		else:
			a = x
			b = 2 * n + 1
			g = gcd(a, b)
			print(a // g, end=' ')
			print(b // g)


import math
t = int(input())
for _ in range(t):
	(n, tt, x, y, z) = map(int, input().split())
	val = 2 * n + 1
	if tt == 2 or tt == 4:
		y = val - 2 * y
		gc = math.gcd(y, val)
		y //= gc
		val //= gc
	if tt == 1 or tt == 3:
		if y > x and z < y:
			gc = math.gcd(val, y - 1)
			y = (y - 1) // gc
			val //= gc
		elif y > x and z > y and (tt == 1):
			gc = math.gcd(val - y - 1, val)
			y = (val - y - 1) // gc
			val //= gc
		elif y < x and z > y:
			gc = math.gcd(y + 1, val)
			y = (y + 1) // gc
			val //= gc
		elif y < x and z < y and (tt == 1):
			gc = math.gcd(val - y + 1, val)
			y = (val - y + 1) // gc
			val //= gc
		elif y > x and z > y and (tt == 3):
			gc = math.gcd(val - y + 1, val)
			y = (val - y + 1) // gc
			val //= gc
		elif y < x and z < y and (tt == 3):
			gc = math.gcd(val - y - 1, val)
			y = (val - y - 1) // gc
			val //= gc
	print(y, val)


from fractions import Fraction

def func1(x, y, z, n):
	w = 2 * n + 1
	ans = Fraction(180, w)
	if x < y and y > z:
		ans = ans * (y - 1)
	elif x > y and y < z:
		ans = ans * (y + 1)
	elif x < y and y < z:
		inter = 4 * y * ans
		ans = (360 - inter) / 2 + (y - 1) * ans
	elif x > y and y > z:
		inter = 4 * y * ans
		ans = (360 - inter) / 2 + (y + 1) * ans
	ans = ans / 180
	return [ans.numerator, ans.denominator]

def func2(x, y, z, n):
	w = 2 * n + 1
	ans = Fraction(180, w)
	inter = 4 * y * ans
	ans = (360 - inter) / 2
	ans = ans / 180
	return [ans.numerator, ans.denominator]

def func3(x, y, z, n):
	w = 2 * n + 1
	ans = Fraction(180, w)
	if x < y and y > z:
		ans = (y - 1) * ans
	elif x > y and y < z:
		ans = (y + 1) * ans
	elif x > y and y > z:
		inter = 4 * y * ans
		ans = (360 - inter) / 2 + (y - 1) * ans
	elif x < y and y < z:
		inter = 4 * y * ans
		ans = (360 - inter) / 2 + (y + 1) * ans
	ans = ans / 180
	return [ans.numerator, ans.denominator]
t = int(input())
for _ in range(t):
	(n, t, x, y, z) = map(int, input().split())
	if t == 1:
		print(*func1(x, y, z, n))
	elif t == 2:
		print(*func2(x, y, z, n))
	elif t == 3:
		print(*func3(x, y, z, n))
	else:
		print(*func2(x, y, z, n))


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
for _ in range(int(input())):
	(n, t, x, y, z) = map(int, input().split())
	b = 2 * n + 1
	if t == 1:
		if x < y:
			if z > y:
				a = 2 * n + 1 - z
			else:
				a = z
		elif z < y:
			a = 2 * n + 1 - z
		else:
			a = z
	elif t == 2:
		a = 2 * n + 1 - 2 * y
	elif t == 3:
		if x < y:
			if z > y:
				a = 2 * n + 1 - x
			else:
				a = x
		elif z < y:
			a = 2 * n + 1 - x
		else:
			a = x
	else:
		a = 2 * n + 1 - 2 * y
	q = gcd(a, b)
	print(a // q, b // q)


from fractions import Fraction

def findAngle(Type, x, y, z, angle):
	if Type == 1:
		if y == x + 1 and z == y - 1:
			return x * angle
		elif y == x + 1 and z == y + 1:
			return 1 - z * angle
		elif y == x - 1 and z == y + 1:
			return x * angle
		elif y == x - 1 and z == y - 1:
			return 1 - z * angle
	elif Type == 2:
		if y == x + 1 and z == y + 1:
			return 1 - angle * 2 * y
		elif y == x - 1 and z == y - 1:
			return 1 - angle * 2 * y
	elif Type == 3:
		if y == x + 1 and z == y - 1:
			return x * angle
		elif y == x - 1 and z == y + 1:
			return x * angle
		elif y == x + 1 and z == y + 1:
			return 1 - angle * x
		elif y == x - 1 and z == y - 1:
			return 1 - angle * x
	elif Type == 4:
		if y == x + 1 and z == y + 1:
			return 1 - angle * 2 * y
		elif y == x - 1 and z == y - 1:
			return 1 - angle * 2 * y
T = int(input())
for i in range(T):
	(n, t, x, y, z) = list(map(int, input().split(' ')))
	angle = Fraction(1, 2 * n + 1)
	ans = findAngle(t, x, y, z, angle)
	print(ans.numerator, ans.denominator)


import math
tt = int(input())
for _ in range(tt):
	(n, t, x, y, z) = map(int, input().split(' '))
	p = 0
	q = 0
	temp = 0
	q = 2 * n + 1
	if x == z:
		if t == 1:
			p = x
		elif t == 2 or t == 4:
			p = 0
		else:
			p = x
	elif t == 1:
		p = 2 * n + 1 - z
	elif t == 2 or t == 4:
		p = 2 * n + 1 - x - z
	else:
		p = 2 * n + 1 - x
	temp = math.gcd(p, q)
	p //= temp
	q //= temp
	print(str(p) + ' ' + str(q))


def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	elif a > b:
		return gcd(a % b, b)
	elif b >= a:
		return gcd(b % a, a)
T = int(input())
for i in range(0, T):
	inp = input()
	li = inp.split()
	n = int(li[0])
	t = int(li[1])
	x = int(li[2])
	y = int(li[3])
	z = int(li[4])
	mux = 0
	angle = 1 + 2 * n
	if t == 1:
		if z > y > x:
			m = angle - (y + 1)
			g = gcd(angle, m)
			m = int(m / g)
			angle = int(angle / g)
			print('{} {}'.format(m, angle))
		elif x < y and z < y:
			m = y - 1
			g = gcd(angle, m)
			angle = int(angle / g)
			m = int(m / g)
			print('{} {}'.format(m, angle))
		elif x > y and z > y:
			m = y + 1
			g = gcd(angle, m)
			m = int(m / g)
			angle = int(angle / g)
			print('{} {}'.format(m, angle))
		elif x > y > z:
			m = angle - y + 1
			g = gcd(angle, m)
			m = int(m / g)
			angle = int(angle / g)
			print('{} {}'.format(m, angle))
	elif t == 2:
		m = angle - 2 * y
		g = gcd(angle, m)
		m = int(m / g)
		angle = int(angle / g)
		print('{} {}'.format(m, angle))
	elif t == 3:
		if z > y > x:
			m = angle - (y - 1)
			g = gcd(angle, m)
			m = int(m / g)
			angle = int(angle / g)
			print('{} {}'.format(m, angle))
		elif y > x and z < y:
			m = y - 1
			g = gcd(angle, m)
			m = int(m / g)
			angle = int(angle / g)
			print('{} {}'.format(m, angle))
		elif y < x and y < z:
			m = y + 1
			g = gcd(angle, m)
			m = int(m / g)
			angle = int(angle / g)
			print('{} {}'.format(m, angle))
		elif z < y < x:
			m = angle - (y + 1)
			g = gcd(angle, m)
			m = int(m / g)
			angle = int(angle / g)
			print('{} {}'.format(m, angle))
	else:
		m = angle - 2 * y
		g = gcd(angle, m)
		m = int(m / g)
		angle = int(angle / g)
		print('{} {}'.format(m, angle))


from fractions import Fraction
t = int(input())

def prin(fra):
	s = str(fra)
	(n, d) = s.split('/')
	print(n + ' ' + d)
one = Fraction(1, 1)
for _ in range(t):
	(n, t, x, y, z) = map(int, input().split())
	alpha = Fraction(1, 2 * n + 1)
	if t == 1:
		if y == 0 and x == 1 and (z == 1):
			prin(alpha)
		else:
			if y > x and z > y:
				prin(one - (y + 1) * alpha)
			if y < x and z > y:
				prin((y + 1) * alpha)
			if y > x and z < y:
				prin((y - 1) * alpha)
			if y < x and z < y:
				prin(one - (y - 1) * alpha)
	if t == 2 or t == 4:
		if x == 0 and z == 0:
			prin(Fraction(0, 1))
		else:
			prin(one - 2 * (y * alpha))
	if t == 3:
		if x == 0 and y == 1 and (z == 2):
			print('1 1')
		else:
			if y > x and z > y:
				prin(one - (y - 1) * alpha)
			if y < x and z > y:
				prin((y + 1) * alpha)
			if y > x and z < y:
				prin((y - 1) * alpha)
			if y < x and z < y:
				prin(one - (y + 1) * alpha)


from fractions import Fraction
for _ in range(int(input())):
	(n, t, x, y, z) = map(int, input().split())
	angle = Fraction(180, 2 * n + 1)
	if t == 1:
		if x < y and y < z:
			angle = (360 - 4 * y * angle) / 2 + (y - 1) * angle
		elif x > y and y > z:
			angle = (360 - 4 * y * angle) / 2 + (y + 1) * angle
		elif x > y and y < z:
			angle = (y + 1) * angle
		elif x < y and y > z:
			angle = (y - 1) * angle
	elif t == 2 or t == 4:
		angle = (360 - 4 * y * angle) / 2
	elif x < y and y < z:
		angle = (360 - 4 * y * angle) / 2 + (y + 1) * angle
	elif x > y and y > z:
		angle = (360 - 4 * y * angle) / 2 + (y - 1) * angle
	elif x > y and y < z:
		angle = (y + 1) * angle
	elif x < y and y > z:
		angle = (y - 1) * angle
	angle /= 180
	print(angle.numerator, angle.denominator)


def euclid_gcd(m, n):
	if n == 0:
		return m
	return euclid_gcd(n, m % n)
for _ in range(int(input())):
	(n, t, x, y, z) = [int(i) for i in input().split()]
	if t == 1:
		if x != z:
			m = 2 * n + 1 - z
			n = 2 * n + 1
		else:
			m = z
			n = 2 * n + 1
	elif t == 2:
		m = 2 * n + 1 - 2 * y
		n = 2 * n + 1
	elif t == 3:
		if x != z:
			m = 2 * n + 1 - x
			n = 2 * n + 1
		else:
			m = x
			n = 2 * n + 1
	else:
		m = 2 * n + 1 - 2 * y
		n = 2 * n + 1
	factor = euclid_gcd(m, n)
	print(m // factor, n // factor)


def gcd(x, y):
	if y == 0:
		return x
	else:
		return gcd(y, x % y)
T = int(input())
for xx in range(T):
	(n, t, x, y, z) = [int(c) for c in input().split()]
	a = 2 * n + 1
	m = 0
	l = 0
	if t == 2:
		if x > y and y > z:
			g = int(gcd(a, 2 * n - 2 * x + 3))
			m = int(a / g)
			l = int(2 * n - 2 * x + 3) / g
		else:
			g = int(gcd(a, 2 * n - 2 * x - 1))
			m = int(a / g)
			l = int(2 * n - 2 * x - 1) / g
	elif t == 1:
		if x > y and y > z:
			g = int(gcd(a, 2 * n - x + 3))
			m = int(a / g)
			l = int(2 * n - x + 3) / g
		elif x == z:
			g = int(gcd(a, x))
			m = int(a / g)
			l = int(x) / g
		else:
			g = int(gcd(a, 2 * n - x - 1))
			m = int(a / g)
			l = int(2 * n - x - 1) / g
	elif t == 4:
		if x > y and y > z:
			g = int(gcd(a, 2 * n - 2 * x + 3))
			m = int(a / g)
			l = int(2 * n - 2 * x + 3) / g
		else:
			g = int(gcd(a, 2 * n - 2 * x - 1))
			m = int(a / g)
			l = int(2 * n - 2 * x - 1) / g
	elif t == 3:
		if x > y and y > z:
			g = int(gcd(a, 2 * n - x + 1))
			m = int(a / g)
			l = int(2 * n - x + 1) / g
		elif x == z:
			g = int(gcd(a, x))
			m = int(a / g)
			l = int(x) / g
		else:
			g = int(gcd(a, 2 * n - x + 1))
			m = int(a / g)
			l = int(2 * n - x + 1) / g
	print(str(int(l)) + ' ' + str(m))


from math import gcd
tt = int(input())
while tt:
	tt -= 1
	(n, t, x, y, z) = list(map(int, input().split()))
	den = 2 * n + 1
	if t != 1:
		if t == 2 and x > z:
			(x, z) = (z, x)
		elif t == 3:
			(x, y, z) = (z, y, x)
		elif t == 4:
			(x, z) = (min(x, z), max(x, z))
	if t == 2 or t == 4:
		if x == n - 1 and y == n and (z == n):
			num = 1
		else:
			num = 2 * n - 2 * y + 1
	elif x == n - 1 and y == n and (z == n):
		num = n
	elif x < y:
		if x == z:
			num = y - 1
		else:
			num = 2 * n - y
	elif x == z:
		num = y + 1
	else:
		num = 2 * n - y + 2
	g = gcd(num, den)
	print(num // g, den // g)


from math import gcd
for _ in range(int(input())):
	(N, t, x, y, z) = map(int, input().split())
	n = 2 * N + 1
	m = [n - 2 * y, [[n - x, n - z][t == 1], x][x == z]][t & 1]
	g = gcd(m, n)
	print(m // g, n // g)


from math import gcd
for _ in range(int(input())):
	(N, t, x, y, z) = map(int, input().split())
	n = 2 * N + 1
	if t % 2 == 0:
		m = n - 2 * y
	elif t % 2 == 1 and x != z:
		m = n - (y + 1) if x < y and t == 1 or (x > y and t == 3) else n - (y - 1)
	elif t % 2 == 1 and x == z:
		m = x
	g = gcd(m, n)
	print(m // g, n // g)


from math import gcd
f = lambda x, n: x + 1
g = lambda x, n: 2 * n + 1 - 2 * x
func = [[], [lambda x, n: f(x, n) + g(x, n), lambda x, n: f(x, n), lambda x, n: f(x - 2, n), lambda x, n: f(x - 2, n) + g(x, n)], [lambda x, n: g(x, n), lambda x, n: 0, lambda x, n: 0, lambda x, n: g(x, n)], [lambda x, n: f(x - 2, n) + g(x, n), lambda x, n: f(x, n), lambda x, n: f(x - 2, n), lambda x, n: f(x, n) + g(x, n)], [lambda x, n: g(x, n), lambda x, n: 0, lambda x, n: 0, lambda x, n: g(x, n)]]
for _ in range(int(input())):
	(N, t, x, y, z) = map(int, input().split())
	m = func[t][[[3, 0][x > z], [2, 1][x > y]][x == z]](y, N)
	t = gcd(m, 2 * N + 1)
	print(int(m / t), int((2 * N + 1) / t))


from math import gcd
T = int(input())
while T > 0:
	T = T - 1
	(n, t, x, y, z) = map(int, input().split())
	c = 2 * n + 1
	if t == 1:
		if x - y == 1 and y - z == 1:
			d1 = 2 * n - y + 2
			d2 = 2 * n + 1
		elif x - y == -1 and y - z == -1:
			d1 = 2 * n - y
			d2 = 2 * n + 1
		elif x - y == 1 and y - z == -1:
			d1 = y + 1
			d2 = 2 * n + 1
		elif x - y == -1 and y - z == 1:
			d1 = y - 1
			d2 = 2 * n + 1
	elif t == 2:
		d1 = 2 * n + 1 - 2 * y
		d2 = 2 * n + 1
	elif t == 3:
		if y - z == 1 and x - y == -1:
			d1 = y - 1
			d2 = 2 * n + 1
		elif y - z == -1 and x - y == -1:
			d1 = 2 * n + 1 - (y - 1)
			d2 = 2 * n + 1
		elif y - z == 1 and x - y == 1:
			d1 = 2 * n + 1 - (y + 1)
			d2 = 2 * n + 1
		elif y - z == -1 and x - y == 1:
			d1 = y + 1
			d2 = 2 * n + 1
	else:
		d1 = 2 * n + 1 - 2 * y
		d2 = 2 * n + 1
	if gcd(d1, d2) != 1:
		c = 1
	else:
		c = 0
	while c != 0:
		x = gcd(d1, d2)
		d1 = d1 // x
		d2 = d2 // x
		if x == 1:
			c = 0
	print(d1, d2)


t = int(input())

def gcd(a, b):
	if a == 0:
		return b
	else:
		return gcd(b % a, a)
for i in range(t):
	(n, t, x, y, z) = [int(x) for x in input().split()]
	if t == 1:
		if x < y and y < z:
			print((2 * n + 1 - z) // gcd(2 * n + 1 - z, 2 * n + 1), (2 * n + 1) // gcd(2 * n + 1 - z, 2 * n + 1))
		elif x < y and y > z:
			print(x // gcd(x, 2 * n + 1), (2 * n + 1) // gcd(x, 2 * n + 1))
		elif x > y and y < z:
			print((y + 1) // gcd(y + 1, 2 * n + 1), (2 * n + 1) // gcd(x, 2 * n + 1))
		elif x > y and y > z:
			print((2 * n + 1 - z) // gcd(2 * n + 1 - z, 2 * n + 1), (2 * n + 1) // gcd(2 * n + 1 - z, 2 * n + 1))
		elif x < y and y == z and (y == n):
			print(1, 2 * n + 1)
	elif t == 2:
		if x < y and y < z or (x > y and y > x):
			print((2 * n + 1 - 2 * y) // gcd(2 * n + 1 - 2 * y, 2 * n + 1), (2 * n + 1) // gcd(2 * n + 1 - 2 * y, 2 * n + 1))
		elif x < y and y == z and (y == n):
			print(1, 2 * n + 1)
	elif t == 3:
		if x < y and y < z:
			print((2 * n + 1 - x) // gcd(2 * n + 1 - x, 2 * n + 1), (2 * n + 1) // gcd(2 * n + 1 - x, 2 * n + 1))
		elif x < y and y > z:
			print(x // gcd(x, 2 * n + 1), (2 * n + 1) // gcd(x, 2 * n + 1))
		elif x > y and y < z:
			print(x // gcd(x, 2 * n + 1), (2 * n + 1) // gcd(x, 2 * n + 1))
		elif x > y and y > z:
			print((2 * n + 1 - x) // gcd(2 * n + 1 - x, 2 * n + 1), (2 * n + 1) // gcd(2 * n + 1 - x, 2 * n + 1))
		elif x == y and y > z and (y == n):
			print(y // gcd(y, 2 * n + 1), (2 * n + 1) // gcd(y, 2 * n + 1))
	elif x < y and y < z or (z > y and y > x):
		print((2 * n + 1 - 2 * y) // gcd(2 * n + 1 - 2 * y, 2 * n + 1), (2 * n + 1) // gcd(2 * n + 1 - 2 * y, 2 * n + 1))
	elif x == y and y > z and (y == n):
		print(1, 2 * n + 1)


def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)
T = int(input())
for u in range(T):
	(N, t, x, y, z) = list(map(int, input().split()))
	n = N + N + 1
	m = 1
	if t == 1 or t == 3:
		if x == z:
			if x < y:
				m = (y - 1) * m
			elif x > y:
				m = (y + 1) * m
		elif x < y:
			if t == 1:
				m = n - (1 + y) * m
			else:
				m = n - (y - 1) * m
		elif t == 3:
			m = n - (1 + y) * m
		else:
			m = n - (y - 1) * m
	elif t == 2 or t == 4:
		m = n - 2 * y * m
	hcf = gcd(m, n)
	print(str(m // hcf), str(n // hcf))


import math as m
t = int(input())
for i in range(t):
	[n, t, x, y, z] = list(map(int, input().split()))
	k = 2 * n + 1
	if len(list(set([x, y, z]))) == 3:
		if t == 1:
			if x > y:
				g = m.gcd(k - (y - 1), k)
				h = k // g
				l = (k - (y - 1)) // g
				print(l, h)
			else:
				g = m.gcd(k - (y + 1), k)
				h = k // g
				l = (k - (y + 1)) // g
				print(l, h)
		if t == 3:
			g = m.gcd(k - x, k)
			h = k // g
			l = (k - x) // g
			print(l, h)
		if t == 2 or t == 4:
			g = m.gcd(k - (x + z), k)
			h = k // g
			l = (k - (x + z)) // g
			print(l, h)
	elif t == 1 or t == 3:
		g = m.gcd(x, k)
		print(x // g, k // g)


def gcd(a, b):
	if a % b == 0:
		return b
	else:
		return gcd(b, a % b)
t = int(input())
while t:
	(n, type, x, y, z) = map(int, input().split())
	a = 2 * n + 1
	if type == 1:
		if y > x and z == x:
			m = y - 1
			g = gcd(m, a)
			m = m / g
			d = a / g
			print(int(m), ' ', int(d))
		elif y > x and z == y + 1:
			m = a - y - 1
			g = gcd(m, a)
			m = m / g
			d = a / g
			print(int(m), ' ', int(d))
		elif x > y and x == z:
			m = y + 1
			g = gcd(m, a)
			m = m / g
			d = a / g
			print(int(m), ' ', int(d))
		elif x > y and z == y - 1:
			m = a - y + 1
			g = gcd(m, a)
			m = m / g
			d = a / g
			print(int(m), ' ', int(d))
	elif type == 2:
		m = a - 2 * y
		g = gcd(m, a)
		m = m / g
		d = a / g
		print(int(m), ' ', int(d))
	elif type == 3:
		if y > x and z == x:
			m = y - 1
			g = gcd(m, a)
			m = m / g
			d = a / g
			print(int(m), ' ', int(d))
		elif y > x and z == y + 1:
			m = a - y + 1
			g = gcd(m, a)
			m = m / g
			d = a / g
			print(int(m), ' ', int(d))
		elif x > y and x == z:
			m = y + 1
			g = gcd(m, a)
			m = m / g
			d = a / g
			print(int(m), ' ', int(d))
		elif x > y and z == y - 1:
			m = a - y - 1
			g = gcd(m, a)
			m = m / g
			d = a / g
			print(int(m), ' ', int(d))
	elif type == 4:
		m = a - 2 * y
		g = gcd(m, a)
		m = m / g
		d = a / g
		print(int(m), ' ', int(d))
	t -= 1


def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
for _ in range(int(input())):
	(n, t, x, y, z) = map(int, input().split())
	a = 2 * n + 1
	if t == 1:
		if y != n:
			if x > y:
				if y > z:
					b = a - y + 1
					g = gcd(a, b)
					m = b / g
					n = a / g
				elif y < z:
					b = y + 1
					g = gcd(a, b)
					m = b / g
					n = a / g
			elif x < y:
				if y > z:
					b = y - 1
					g = gcd(a, b)
					m = b / g
					n = a / g
				elif y < z:
					b = a - y - 1
					g = gcd(a, b)
					m = b / g
					n = a / g
		else:
			b = n
			g = gcd(a, b)
			m = b / g
			n = a / g
	elif t == 2 or t == 4:
		if y != n:
			b = a - 2 * y
			g = gcd(a, b)
			m = b / g
			n = a / g
		else:
			b = 1
			g = gcd(a, b)
			m = b / g
			n = a / g
	elif t == 3:
		if y == n:
			b = n - 1
			g = gcd(a, b)
			m = b / g
			n = a / g
		elif x > y:
			if y > z:
				b = a - y - 1
				g = gcd(a, b)
				m = b / g
				n = a / g
			elif y < z:
				b = y + 1
				g = gcd(a, b)
				m = b / g
				n = a / g
		elif x < y:
			if y > z:
				b = y - 1
				g = gcd(a, b)
				m = b / g
				n = a / g
			elif y < z:
				b = a - y + 1
				g = gcd(a, b)
				m = b / g
				n = a / g
	print(str(int(m)) + ' ' + str(int(n)))


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
t1 = int(input())
for i in range(t1):
	(n, t, x, y, z) = map(int, input().split())
	if t == 1:
		if z != x:
			a = 2 * n + 1 - z
			b = 2 * n + 1
		elif z == x:
			a = x
			b = 2 * n + 1
	elif t == 2:
		a = 2 * n + 1 - 2 * y
		b = 2 * n + 1
	elif t == 3:
		if z != x:
			a = 2 * n + 1 - x
			b = 2 * n + 1
		elif z == x:
			a = x
			b = 2 * n + 1
	elif t == 4:
		a = 2 * n + 1 - 2 * y
		b = 2 * n + 1
	c = gcd(a, b)
	print(a // c, b // c)


def gcd(a, b):
	rem = b % a
	while rem != 0:
		b = a
		a = rem
		rem = b % a
	return a
T = int(input())
for test in range(0, T):
	(n, t, x, y, z) = input().split(' ')
	n = int(n)
	t = int(t)
	x = int(x)
	y = int(y)
	z = int(z)
	if t == 1:
		if y == x + 1 and z == y + 1:
			div = gcd(2 * n + 1 - z, 2 * n + 1)
			print((2 * n + 1 - z) // div, (2 * n + 1) // div)
		if y == x + 1 and z == y - 1:
			div = gcd(z, 2 * n + 1)
			print(z // div, (2 * n + 1) // div)
		if y == x - 1 and z == y + 1:
			div = gcd(z, 2 * n + 1)
			print(z // div, (2 * n + 1) // div)
		if y == x - 1 and z == y - 1:
			div = gcd(2 * n + 1 - z, 2 * n + 1)
			print((2 * n + 1 - z) // div, (2 * n + 1) // div)
	elif t == 2:
		div = gcd(2 * n + 1 - 2 * y, 2 * n + 1)
		print((2 * n + 1 - 2 * y) // div, (2 * n + 1) // div)
	elif t == 3:
		if y == x + 1 and z == y + 1:
			div = gcd(2 * n + 1 - x, 2 * n + 1)
			print((2 * n + 1 - x) // div, (2 * n + 1) // div)
		if y == x + 1 and z == y - 1:
			div = gcd(x, 2 * n + 1)
			print(x // div, (2 * n + 1) // div)
		if y == x - 1 and z == y + 1:
			div = gcd(x, 2 * n + 1)
			print(x // div, (2 * n + 1) // div)
		if y == x - 1 and z == y - 1:
			div = gcd(2 * n + 1 - x, 2 * n + 1)
			print((2 * n + 1 - x) // div, (2 * n + 1) // div)
	else:
		div = gcd(2 * n + 1 - 2 * y, 2 * n + 1)
		print((2 * n + 1 - 2 * y) // div, (2 * n + 1) // div)


import math
for _ in range(int(input())):
	(n, t, x, y, z) = [int(x) for x in input().split()]
	N = 2 * n
	f = math.gcd(y - 1, N + 1)
	a = [(y - 1) // f, (N + 1) // f]
	f = math.gcd(N - y, N + 1)
	b = [(N - y) // f, (N + 1) // f]
	f = math.gcd(N + 1 - 2 * y, N + 1)
	c = [(N + 1 - 2 * y) // f, (N + 1) // f]
	f = math.gcd(N + 2 - y, N + 1)
	d = [(N + 2 - y) // f, (N + 1) // f]
	f = math.gcd(y + 1, N + 1)
	e = [(y + 1) // f, (N + 1) // f]
	ans = []
	if t == 1:
		if y - x == 1:
			if x == z:
				ans = a
			else:
				ans = b
		if x - y == 1:
			if x == z:
				ans = e
			else:
				ans = d
	if t == 2:
		if y - x == 1:
			if x == z:
				ans = [0, 0]
			else:
				ans = c
		if x - y == 1:
			if x == z:
				ans = [0, 0]
			else:
				ans = c
	if t == 3:
		if y - x == 1:
			if x == z:
				ans = a
			else:
				ans = d
		if x - y == 1:
			if x == z:
				ans = e
			else:
				ans = b
	if t == 4:
		if y - x == 1:
			if x == z:
				ans = 0
			else:
				ans = c
		if x - y == 1:
			if x == z:
				ans = 0
			else:
				ans = c
	print(*ans)


def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
t1 = int(input())
i = 0
while i < t1:
	l = [int(x) for x in input().split()]
	n = l[0]
	t = l[1]
	x = l[2]
	y = l[3]
	z = l[4]
	if t == 1:
		if z > y and y > x:
			s = gcd(2 * n - y, 2 * n + 1)
			print((2 * n - y) // s, (2 * n + 1) // s)
		if y > x and z < y:
			s = gcd(y - 1, 2 * n + 1)
			print((y - 1) // s, (2 * n + 1) // s)
		if x > y and z > y:
			s = gcd(y + 1, 2 * n + 1)
			print((y + 1) // s, (2 * n + 1) // s)
		if x > y and y > z:
			s = gcd(2 * n - y + 2, 2 * n + 1)
			print((2 * n - y + 2) // s, (2 * n + 1) // s)
	elif t == 3:
		if x > y and y > z:
			s = gcd(2 * n - y, 2 * n + 1)
			print((2 * n - y) // s, (2 * n + 1) // s)
		if x > y and z > y:
			s = gcd(y + 1, 2 * n + 1)
			print((y + 1) // s, (2 * n + 1) // s)
		if y > x and z > y:
			s = gcd(2 * n - y + 2, 2 * n + 1)
			print((2 * n - y + 2) // s, (2 * n + 1) // s)
		if y > x and z < y:
			s = gcd(y - 1, 2 * n + 1)
			print((y - 1) // s, (2 * n + 1) // s)
	elif t == 2 or t == 4:
		if x < y and y < z or (x > y and y > z):
			s = gcd(2 * n - 2 * y + 1, 2 * n + 1)
			print((2 * n - 2 * y + 1) // s, (2 * n + 1) // s)
	i += 1


import math
t = int(input())
for T in range(t):
	(N, t, x, y, z) = [int(ele) for ele in input().split()]
	xyzmin = min(x, y, z) - 1
	query = int(str(x - xyzmin) + str(y - xyzmin) + str(z - xyzmin))
	term1 = 2 * N + 1
	term2 = 0
	if t == 1:
		if query == 121 or query == 212:
			term2 = z
		elif query == 321 or query == 123:
			term2 = term1 - z
	elif t == 2:
		if query == 321 or query == 123:
			term2 = term1 - x - z
	elif t == 3:
		if query == 121 or query == 212:
			term2 = z
		elif query == 321 or query == 123:
			term2 = term1 - x
	elif query == 321 or query == 123:
		term2 = term1 - x - z
	getgcd = math.gcd(term1, term2)
	term1 /= getgcd
	term2 /= getgcd
	print(str(int(term2)) + ' ' + str(int(term1)))


import sys
import math
t = int(input())

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
for j in range(0, t):
	(n, m, x, y, z) = map(int, sys.stdin.readline().split(' '))
	n2 = 2 * n + 1
	if m == 1:
		if x - y == 1 and y - z == -1:
			n1 = z
		if x - y == 1 and y - z == 1:
			n1 = 2 * n + 1 - z
		if x - y == -1 and y - z == 1:
			n1 = x
		if x - y == -1 and y - z == -1:
			n1 = 2 * n + 1 - z
	if m == 3:
		if x - y == 1 and y - z == -1:
			n1 = z
		if x - y == 1 and y - z == 1:
			n1 = 2 * n + 1 - x
		if x - y == -1 and y - z == 1:
			n1 = x
		if x - y == -1 and y - z == -1:
			n1 = 2 * n + 1 - x
	if m == 4 or m == 2:
		n1 = 2 * n + 1 - 2 * y
	nn = n1 / gcd(n1, n2)
	mm = n2 / gcd(n1, n2)
	print(int(nn), int(mm))


from math import *
for test in range(int(input())):
	[n, t, x, y, z] = list(map(int, input().split()))
	if t == 4 or t == 2:
		[m1, n1] = [2 * n + 1 - y * 2, 2 * n + 1]
		c = gcd(m1, n1)
		(m1, n1) = (m1 // c, n1 // c)
		print(m1, n1)
	if t == 1 or t == 3:
		if x == z:
			[m1, n1] = [x, 2 * n + 1]
			c = gcd(m1, n1)
			(m1, n1) = (m1 // c, n1 // c)
			print(m1, n1)
		elif t == 3:
			[m1, n1] = [2 * n + 1 - x, 2 * n + 1]
			c = gcd(m1, n1)
			(m1, n1) = (m1 // c, n1 // c)
			print(m1, n1)
		else:
			[m1, n1] = [2 * n + 1 - z, 2 * n + 1]
			c = gcd(m1, n1)
			(m1, n1) = (m1 // c, n1 // c)
			print(m1, n1)