Problem:
Distance

For given two segments s1 and s2, print the distance between them.

s1 is formed by end points p0 and p1, and
s2 is formed by end points p2 and p3.

Input

The entire input looks like:

q (the number of queries)
1st query
2nd query
...
qth query

Each query consists of integer coordinates of end points of s1 and s2 in the following format:

xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3

Output

For each query, print the distance. The output values should be in a decimal fraction with an error less than 0.00000001.

Constraints

1 ≤ q ≤ 1000

-10000 ≤ xpi, ypi ≤ 10000

p0 ≠ p1 and
p2 ≠ p3.

Sample Input

3
0 0 1 0 0 1 1 1
0 0 1 0 2 1 1 2
-1 0 1 0 0 1 0 -1

Sample Output

1.0000000000
1.4142135624
0.0000000000

Solution in Python3 (judge verdict: Accepted)

import math

class Vector:
    def __init__(self,x,y):
        self.x = x
        self.y = y
        
    def __add__(self,other):
        return Vector(self.x+other.x,self.y+other.y)
    
    def __sub__(self,other):
        return Vector(self.x-other.x,self.y-other.y)
    
    def __mul__(self,scalar):
        return Vector(self.x*scalar,self.y*scalar)
    def __rmul__(self, scalar):
        return Vector(self.x*scalar,self.y*scalar)
   
    def __repr__(self):
        return str([self.x,self.y])
        
    def norm_2(self):
        return dot(self,self)
    
    def norm(self):
        return math.sqrt(self.norm_2())
    
def v_sum(v1,v2):
    return Vector(v1.x+v2.x,v1.y+v2.y) 
def scalar_multi(k,v):
    return Vector(k*v.x,k*v.y)
def v_diff(v1,v2):
    return v_sum(v1,scalar_multi(-1,v2))
    
def dot(vector1,vector2):
    return vector1.x*vector2.x+vector1.y*vector2.y

def cross(vector1,vector2):
    return vector1.x*vector2.y-vector1.y*vector2.x
    
    
def in_seg(p,s,t):
    '''
    whether point p is in the area of segment(s,t)
    '''
    x = p-s
    d = t-s
    if 0<=dot(x,d)/d.norm_2()<=1:
        return True
    else:
        return False
    
def is_crossed(p,q,s,t):
    a = q-p
    b = t-s
    v = s-p
    w = t-p
    x = p-s
    y = q-s
    if cross(a,v)*cross(a,w)<=0 and cross(b,x)*cross(b,y)<=0:
        if cross(a,v) == cross(a,w) == cross(b,x) == cross(b,y) == 0: 
            #if segments are on the same line, you must check whether an end point is in the other segment at least
            if 0<=dot(a,v)<=a.norm_2() or 0<=dot(a,w)<=a.norm_2() or 0<=dot(b,x)<=b.norm_2() or 0<=dot(b,y)<=b.norm_2():
                return True
            else:
                return False
        else:    
            return True
    else:
        return False
    
def point_line_dist(p,s,t):
    x = p-s
    d = t-s
    return abs(cross(x,d))/d.norm()

def calc(p1,p2,p3,p4):
    min_dists = []
    if in_seg(p1,p3,p4):
        min_dists.append(point_line_dist(p1,p3,p4))
    if in_seg(p2,p3,p4):
        min_dists.append(point_line_dist(p2,p3,p4))
    if in_seg(p3,p1,p2):
        min_dists.append(point_line_dist(p3,p1,p2))
    if in_seg(p4,p1,p2):
        min_dists.append(point_line_dist(p4,p1,p2))
    
    min_dists.append((p1-p3).norm())
    min_dists.append((p1-p4).norm())
    min_dists.append((p2-p3).norm())
    min_dists.append((p2-p4).norm())
    
    if is_crossed(p1,p2,p3,p4):
        min_dists.append(0)
    
    return min(min_dists)

q = int(input())

for i in range(q):
    x1,y1,x2,y2,x3,y3,x4,y4 = map(float,input().split())
    p1 = Vector(x1,y1)
    p2 = Vector(x2,y2)
    p3 = Vector(x3,y3)
    p4 = Vector(x4,y4)
    print(calc(p1,p2,p3,p4))