You are given two points $P$ and $Q$ and an opaque sphere in a three-dimensional space. The point $P$ is not moving, while $Q$ is moving in a straight line with constant velocity. You are also given a direction vector $d$ with the following meaning: the position of $Q$ at time $t$ is $Q(t) = Q(0) + d \cdot t$, where $Q(0)$ is the initial position of $Q$.
It is guaranteed that $Q$ is not visible from $P$ initially (at time $t=0$). It is also guaranteed that $P$ and $Q$ do not touch the sphere at any time.
Find the smallest positive time $t_v$ when $Q$ is visible from $P$, i.e. when the line segment connecting points $P$ and $Q$ does not intersect the sphere.

-----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 13 space-separated integers.
- The first three integers $P_x, P_y, P_z$ denote the coordinates of $P$.
- The next three integers $Q_x, Q_y, Q_z$ denote the initial coordinates of $Q$.
- The next three integers $d_x, d_y, d_z$ denote the components of the direction vector $d$.
- The last four integers $c_x, c_y, c_z, r$ denote the coordinates of the centre of the sphere and its radius.

-----Output-----
For each test case, print a single line containing one real number — the time $t_v$. Your answer will be considered correct if its absolute or relative error does not exceed $10^{-6}$. It is guaranteed that $t_v$ exists and does not exceed $10^9$.

-----Constraints-----
- $1 \le T \le 10^5$
- the absolute values of coordinates of all points do not exceed $2\cdot10^9$
- $1 \le r \le 10^9$

-----Subtasks-----
Subtask #1 (25 points): $P_z = Q_z = d_z = c_z = 0$
Subtask #2 (75 points): original constraints

-----Example Input-----
1
3 0 0 -10 -10 0 0 10 0 0 -3 0 3

-----Example Output-----
1.0000000000

Difficulty: MEDIUM_HARD

Solutions:
from math import *
T = int(input())
for _ in range(T):
	(px, py, pz, qx, qy, qz, dx, dy, dz, cx, cy, cz, r) = map(int, input().split())
	M = px - cx
	N = py - cy
	P = pz - cz
	PT2 = M ** 2 + N ** 2 + P ** 2 - r ** 2
	a = (-2 * dx * M - 2 * dy * N - 2 * dz * P) ** 2 - 4 * PT2 * (dx ** 2 + dy ** 2 + dz ** 2)
	b = -1 * ((4 * dx * M + 4 * dy * N + 4 * dz * P) * (px ** 2 + py ** 2 + pz ** 2 - cx ** 2 - cy ** 2 - cz ** 2 - 2 * (qx * M + qy * N + qz * P) + r ** 2 - PT2) + 4 * PT2 * (2 * qx * dx + 2 * qy * dy + 2 * qz * dz - 2 * cx * dx - 2 * cy * dy - 2 * cz * dz))
	c = (px ** 2 + py ** 2 + pz ** 2 - cx ** 2 - cy ** 2 - cz ** 2 - 2 * (qx * M + qy * N + qz * P) + r ** 2 - PT2) ** 2 - 4 * PT2 * (cx ** 2 + cy ** 2 + cz ** 2 + qx ** 2 + qy ** 2 + qz ** 2 - 2 * cx * qx - 2 * cy * qy - 2 * cz * qz - r ** 2)
	ANS = 0
	if a == 0:
		ANS = -c / b
	else:
		D = (b ** 2 - 4 * a * c) ** (1 / 2)
		first = (-1 * b - D) / (2 * a)
		zz = 0
		second = (-1 * b + D) / (2 * a)
		minn = min(second, first)
		if minn < 0:
			if minn == first:
				zz = second
			else:
				zz = first
		ANS = zz
	print('%.10f' % ANS)


from math import sqrt
t = int(input())
for test in range(t):
	(px, py, pz, qx, qy, qz, dx, dy, dz, cx, cy, cz, r) = map(int, input().split())
	c2 = qx - px
	c3 = qy - py
	c4 = qz - pz
	c5 = px - cx
	c6 = py - cy
	c7 = pz - cz
	c1 = c5 * c5 + c6 * c6 + c7 * c7 - r * r
	a = c5 * c5 * dx * dx + c6 * c6 * dy * dy + c7 * c7 * dz * dz + 2 * c5 * c6 * dx * dy + 2 * c6 * c7 * dy * dz + 2 * c5 * c7 * dx * dz - c1 * (dx * dx + dy * dy + dz * dz)
	b = 2 * c2 * dx * c5 * c5 + 2 * c3 * dy * c6 * c6 + 2 * c4 * dz * c7 * c7 + 2 * (c5 * c6 * (c2 * dy + c3 * dx) + c6 * c7 * (c3 * dz + c4 * dy) + c5 * c7 * (c4 * dx + c2 * dz)) - c1 * (2 * c2 * dx + 2 * c3 * dy + 2 * c4 * dz)
	c = c5 * c5 * c2 * c2 + c6 * c6 * c3 * c3 + c7 * c7 * c4 * c4 + 2 * (c5 * c6 * c2 * c3 + c6 * c7 * c3 * c4 + c5 * c7 * c4 * c2) - c1 * (c2 * c2 + c3 * c3 + c4 * c4)
	if a == 0:
		print('{:.10f}'.format(-1 * (c / b)))
	else:
		zt = sqrt(b * b - 4 * a * c)
		zb = -1 * b
		ans1 = zb + zt
		ans1 /= 2 * a
		ans2 = zb - zt
		ans2 /= 2 * a
		l = [ans1, ans2]
		l.sort()
		if l[0] > 0:
			print('{:.10f}'.format(l[0]))
		else:
			print('{:.10f}'.format(l[1]))


import math
for _ in range(int(input())):
	(a, b, c, e, f, g, e1, f1, g1, x1, y1, z1, k) = map(int, input().split())
	alp = a - x1
	bet = b - y1
	thet = c - z1
	c1 = 2 * e * e1 + 2 * f * f1 + 2 * g * g1 - 2 * a * e1 - 2 * b * f1 - 2 * c * g1
	c2 = e1 * e1 + f1 * f1 + g1 * g1
	c3 = 2 * alp * e1 + 2 * bet * f1 + 2 * thet * g1
	p = e * e + f * f + g * g - 2 * a * e - 2 * b * f - 2 * c * g + a * a + b * b + c * c
	q = 2 * e * alp + 2 * f * bet + 2 * g * thet - 2 * a * alp - 2 * b * bet - 2 * c * thet
	r = alp * alp + bet * bet + thet * thet - k * k
	X = c3 * c3 - 4 * c2 * r
	Y = 2 * q * c3 - 4 * c1 * r
	Z = q * q - 4 * p * r
	sq = float(math.sqrt(Y * Y - 4 * X * Z))
	if X != 0:
		ans1 = float((-Y - sq) / (2 * X))
		ans2 = float((-Y + sq) / (2 * X))
		if ans1 < 0 and ans2 >= 0:
			print('%.10f' % ans2)
		elif ans2 < 0 and ans1 >= 0:
			print('%.10f' % ans1)
		elif ans1 >= 0 and ans2 >= 0:
			print('%.10f' % min(ans1, ans2))
	else:
		print('%.10f' % float(-Z / Y))


from math import *
T = int(input())
for i in range(T):
	(px, py, pz, qx, qy, qz, dx, dy, dz, cx, cy, cz, r) = list(map(int, input().split(' ')))
	(p1x, p2x) = (cx - px, qx - px)
	(p1y, p2y) = (cy - py, qy - py)
	(p1z, p2z) = (cz - pz, qz - pz)
	usel = p1x * p1x + p1y * p1y + p1z * p1z - r ** 2
	dot1 = 2 * ((p2x * dx + p2y * dy + p2z * dz) * usel - (dx * p1x + dy * p1y + dz * p1z) * (p1x * p2x + p1y * p2y + p1z * p2z))
	dot2 = (dx * dx + dy * dy + dz * dz) * usel - (dx * p1x + dy * p1y + dz * p1z) * (dx * p1x + dy * p1y + dz * p1z)
	dot3 = (p2x * p2x + p2y * p2y + p2z * p2z) * usel - (p2x * p1x + p2y * p1y + p2z * p1z) * (p2x * p1x + p2y * p1y + p2z * p1z)
	determ = sqrt(dot1 * dot1 - 4 * dot2 * dot3)
	tn = 0
	tm = 0
	tn = -dot1 + determ
	tm = -dot1 - determ
	if dot2 != 0:
		tn = tn / (2 * dot2)
		tm = tm / (2 * dot2)
		if tn > 0 and tm > 0:
			t = min(tn, tm)
			print('%.10f' % t)
		elif tn > 0 and tm < 0:
			t = tn
			print('%.10f' % t)
		elif tn < 0 and tm > 0:
			t = tm
			print('%.10f' % t)
	else:
		t = -dot3 / dot1
		print('%.10f' % t)


def dot(a, b):
	r1 = a[0] * b[0]
	r2 = a[1] * b[1]
	r3 = a[2] * b[2]
	r = r1 + r2 + r3
	return r
for i in range(int(input())):
	(x1, y1, z1, x2, y2, z2, d1, d2, d3, c1, c2, c3, r) = map(float, input().split())
	t1 = r ** 2 * d1 ** 2 + r ** 2 * d2 ** 2 + r ** 2 * d3 ** 2
	t2 = (d1 * (y1 - c2) - d2 * (x1 - c1)) ** 2 + (d2 * (z1 - c3) - d3 * (y1 - c2)) ** 2 + (d3 * (x1 - c1) - d1 * (z1 - c3)) ** 2
	A = t1 - t2
	t11 = 2 * r ** 2 * ((x2 - x1) * d1 + (y2 - y1) * d2 + (z2 - z1) * d3)
	t22 = 2 * (((x2 - x1) * (y1 - c2) - (y2 - y1) * (x1 - c1)) * (d1 * (y1 - c2) - d2 * (x1 - c1)) + ((y2 - y1) * (z1 - c3) - (z2 - z1) * (y1 - c2)) * (d2 * (z1 - c3) - d3 * (y1 - c2)) + ((z2 - z1) * (x1 - c1) - (x2 - x1) * (z1 - c3)) * (d3 * (x1 - c1) - d1 * (z1 - c3)))
	B = t11 - t22
	t111 = r ** 2 * ((x2 - x1) ** 2 + (y2 - y1) ** 2 + (z2 - z1) ** 2)
	t222 = ((x2 - x1) * (y1 - c2) - (y2 - y1) * (x1 - c1)) ** 2 + ((y2 - y1) * (z1 - c3) - (z2 - z1) * (y1 - c2)) ** 2 + ((z2 - z1) * (x1 - c1) - (x2 - x1) * (z1 - c3)) ** 2
	C = t111 - t222
	if A != 0:
		Z = 2 * A
		t1 = (-1 * B + (B ** 2 - 4 * A * C) ** 0.5) / Z
		t2 = (-1 * B - (B ** 2 - 4 * A * C) ** 0.5) / Z
		t = 0
		q = 0
		if t2 < q:
			t = t1
		elif t1 < q:
			t = t2
		elif t1 >= q and t2 >= q:
			if t1 > t2:
				t = t2
			elif t2 > t1:
				t = t1
		print('%.10f' % t)
	elif A == 0:
		t = -1 * C / B
		print('%.10f' % t)
		continue


import math
for _ in range(int(input())):
	l = [int(i) for i in input().split()]
	(use1, use2, use3, use4, use5, use6) = (0, 0, -l[12] * l[12], 0, 0, 0)
	for i in range(3):
		use1 = use1 + (l[i] - l[i + 3]) * (l[i] - l[i + 9])
		use2 = use2 + (l[i] - l[i + 9]) * l[i + 6]
		use3 = use3 + (l[i] - l[i + 9]) * (l[i] - l[i + 9])
		use4 = use4 + l[i + 6] * l[i + 6]
		use5 = use5 + (l[i + 3] - l[i]) * l[i + 6]
		use6 = use6 + (l[i + 3] - l[i]) * (l[i + 3] - l[i])
	a = 4 * use2 * use2 - 4 * use3 * use4
	b = 8 * use1 * use2 + 8 * use3 * use5
	c = 4 * use1 * use1 - 4 * use3 * use6
	desc = math.sqrt(b * b - 4 * a * c)
	if a == 0:
		k = c / b
		print('%.10f' % k)
	else:
		sol1 = (b + desc) / (2 * a)
		sol2 = (b - desc) / (2 * a)
		if sol1 > 0 and sol2 > 0:
			k = min(sol1, sol2)
			print('%.10f' % k)
		elif sol2 <= 0:
			print('%.10f' % sol1)
		elif sol1 <= 0:
			print('%.10f' % sol2)


import math
t = int(input().strip(' '))
for __ in range(t):
	(px, py, pz, qx, qy, qz, dx, dy, dz, cx, cy, cz, r) = map(int, input().split())
	r10 = qx - px
	r11 = qy - py
	r12 = qz - pz
	r00 = px - cx
	r02 = pz - cz
	r01 = py - cy
	res2 = r00 * r12 - r02 * r10
	res1 = r01 * r12 - r02 * r11
	res3 = r00 * r11 - r01 * r10
	ans3 = r00 * dy - r01 * dx
	ans1 = r01 * dz - r02 * dy
	ans2 = r00 * dz - r02 * dx
	A = ans1 ** 2 + ans2 ** 2 + ans3 ** 2 - r ** 2 * (dx ** 2 + dy ** 2 + dz ** 2)
	B = 2 * (res1 * ans1 + res2 * ans2 + res3 * ans3 - r ** 2 * (r10 * dx + r11 * dy + r12 * dz))
	C = res1 ** 2 + res2 ** 2 + res3 ** 2 - r ** 2 * (r10 ** 2 + r11 ** 2 + r12 ** 2)
	D = B ** 2 - 4 * A * C
	if A < 0:
		A *= -1
		B *= -1
		C *= -1
	ANS = 0
	if A == 0:
		ANS = -1 * (C / B)
	else:
		d = math.sqrt(D)
		ANS = max(-B + d, -B - d)
		ANS = ANS / (2 * A)
	print('%.10f' % ANS)


import math

def ret_N_R(px, py, pz, qx, qy, qz, vx, vy, vz, ox, oy, oz):
	Nx = oy * qz - oz * qy + oz * py - oy * pz
	Ny = oz * qx - ox * qz + ox * pz - oz * px
	Nz = ox * qy - oy * qx + oy * px - ox * py
	Rx = oy * vz - oz * vy
	Ry = oz * vx - ox * vz
	Rz = ox * vy - oy * vx
	return (Nx, Ny, Nz, Rx, Ry, Rz)

def quad_coeff(px, py, pz, qx, qy, qz, vx, vy, vz, cx, cy, cz, r):
	ox = px - cx
	oy = py - cy
	oz = pz - cz
	Dx = qx - px
	Dy = qy - py
	Dz = qz - pz
	(Nx, Ny, Nz, Rx, Ry, Rz) = ret_N_R(px, py, pz, qx, qy, qz, vx, vy, vz, ox, oy, oz)
	a = r * r * (vx * vx + vy * vy + vz * vz) - (Rx * Rx + Ry * Ry + Rz * Rz)
	b = 2 * (r * r * (Dx * vx + Dy * vy + Dz * vz) - (Nx * Rx + Ny * Ry + Nz * Rz))
	c = r * r * (Dx * Dx + Dy * Dy + Dz * Dz) - (Nx * Nx + Ny * Ny + Nz * Nz)
	return (a, b, c)

def pos_root(a, b, c):
	det = math.sqrt(b * b - 4 * a * c)
	if a == 0:
		soln = 1
	else:
		s1 = (-1 * b - det) / (2 * a)
		s2 = (det - b) / (2 * a)
		if s1 >= 0:
			soln = s1
		elif s2 >= 0:
			soln = s2
		else:
			soln = 0
	return soln
T = int(input())
for i in range(0, T):
	(Px, Py, Pz, Qx, Qy, Qz, Vx, Vy, Vz, Cx, Cy, Cz, r) = map(float, input().split())
	(a, b, c) = quad_coeff(Px, Py, Pz, Qx, Qy, Qz, Vx, Vy, Vz, Cx, Cy, Cz, r)
	tm = pos_root(a, b, c)
	print(tm)


from math import sqrt
for i in range(int(input())):
	(px, py, pz, qx, qy, qz, dx, dy, dz, cx, cy, cz, r) = map(int, input().split())
	a = (qx - px) * (cx - px) + (qy - py) * (cy - py) + (qz - pz) * (cz - pz)
	b = dx * (cx - px) + dy * (cy - py) + dz * (cz - pz)
	c = (cx - px) * (cx - px) + (cy - py) * (cy - py) + (cz - pz) * (cz - pz) - r * r
	d = dx * dx + dy * dy + dz * dz
	e = (qx - px) * (qx - px) + (qy - py) * (qy - py) + (qz - pz) * (qz - pz)
	f = 2 * (dx * (qx - px) + dy * (qy - py) + dz * (qz - pz))
	A = b * b - c * d
	B = 2 * a * b - c * f
	C = a * a - c * e
	p = sqrt(B * B - 4 * A * C)
	if A == 0:
		ans = -1 * (C / B)
		print('{0:.10f}'.format(ans))
	else:
		ans = (p - B) / (2 * A)
		ans1 = -1 * ((p + B) / (2 * A))
		if ans > 0 and ans1 > 0:
			print('{0:.10f}'.format(min(ans, ans1)))
		elif ans > 0 and ans1 < 0:
			print('{0:.10f}'.format(ans))
		elif ans < 0 and ans1 > 0:
			print('{0:.10f}'.format(ans1))