#include <iostream>
#include <math.h>
#include <cmath>
#include <iomanip>


using namespace std;

//-------------------------------------------------------------------------------------
/*2点[(x1,y1),(x2,y2)]を入力し二点を通る直線ax+by+cの(a,b,c)を返す,  */
/*x1==x2ならfalseを返す*/
/*falseならばx=cの形の直線*/
void straight_line_passing_through_the_two_points(double x1, double y1, double x2, double y2, double *a, double *b, double *c){
    *a=(y2-y1);
    *b=-(x2-x1);
    *c=y2*(x2-x1)-x2*(y2-y1);
}

//-------------------------------------------------------------------------------------
/*(x,y)を通るa1x+b1y+c1=0に垂直な直線a2x+b2y+c2=0を返す*/
bool vertical_straight_line(double x, double y, double a1, double b1, double c1, double *a2, double *b2, double *c2){
    if(a1*x+b1*y+c1==0)return false;
    *a2 = -b1;
    *b2 = a1;
    *c2 = b1*x - a1*y;
    return true;
}


//-------------------------------------------------------------------------------------
/*2直線 a1x+b1y+c1=0 a2x+b2y+c2=0 の交点 (x,y)を返す*/
/*2直線が平行であるならば0を返す*/
int intersection_and_parallel(double a1, double b1, double c1, double a2, double b2, double c2, double *x, double *y){
    if(a1*b2==a2*b1)return false;//平行の判定
    
    *x = (b1*c2-b2*c1)/(a1*b2-a2*b1);
    *y = (a2*c1-a1*c2)/(a1*b2-a2*b1);
    
    return true;
}

//-------------------------------------------------------------------------------------
/* 
 a1x+b1y+c1=0 と　a2x+b2y+c2=0 が
 平行なら2
 直交なら1
 それ以外は0
 を返す
 */
int judges_whether_parallel_or_perpendicular(double a1, double b1, double c1, double a2, double b2, double c2){
    if(a1*b2 == a2*b1)return 2;
    if(a1*a2 + b1*b2 == 0)return 1;
    return 0;
}


//-------------------------------------------------------------------------------------
/*直線ax+by+c=0に対し　(x1,y1)と線対称な位置(x2,y2)を出力*/
/*vertical_straight_lineを使用*/
/*intersection_and_parallelを使用*/
void line_symmetry(double a, double b, double c, double x1, double y1, double *x2, double *y2){
    //垂直な直線を求める
    double a2,b2,c2;
    vertical_straight_line(x1, y1, a, b, c, &a2, &b2, &c2);
    
    //垂直な直線との交点を求める
    double c_x, c_y;
    intersection_and_parallel(a, b, c, a2, b2, c2, &c_x, &c_y);
    *x2 = x1 + 2*(c_x-x1);
    *y2 = y1 + 2*(c_y-y1);
}



//-------------------------------------------------------------------------------------
/*2点(x1,y1), (x2,y2) の距離*/
double distance_between_the_two_pointss(double x1,double y1, double x2, double y2){
    return  sqrt(fabs((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));
}


//-------------------------------------------------------------------------------------
/* (x1,y1)から(x2,y2)へ伸ばした直線の角度を求める  */
double angle_of_the_line(double x1, double y1, double x2, double y2){
    return atan2(y2-y1, x2-x1);
}

//-------------------------------------------------------------------------------------
/*(x1,y1)と(x2,y2)を両端に持つ線分と*/
/*(x3,y3)と(x4,y4)を両端に持つ線分が*/
/*交差するなら1 交差しないなら0を出力する*/
/* straight_line_passing_through_the_two_pointsを使用 */
/* intersection_and_parallelを使用 */
int line_segment_cross_decision(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4){
    double a1,b1,c1, a2,b2,c2;
    straight_line_passing_through_the_two_points(x1, y1, x2, y2, &a1, &b1, &c1);
    straight_line_passing_through_the_two_points(x3, y3, x4, y4, &a2, &b2, &c2);
    double c_x,c_y;
    bool is_parallel = intersection_and_parallel(a1, b1, c1, a2, b2, c2, &c_x, &c_y);
    if(is_parallel==false){
        
        if(a1==a2&&b1==b2&&c1==c2){//一直線上にある.
            
            if(min(x1,x2)<=x3&&x3<=max(x1,x2)&&min(y1,y2)<=y3&&y3<=max(y1,y2))return 1;
            if(min(x1,x2)<=x4&&x4<=max(x1,x2)&&min(y1,y2)<=y4&&y4<=max(y1,y2))return 1;
            if(min(x3,x4)<=x1&&x1<=max(x3,x4)&&min(y3,y4)<=y1&&y1<=max(y3,y4))return 1;
            if(min(x3,x4)<=x2&&x2<=max(x3,x4)&&min(y3,y4)<=y2&&y2<=max(y3,y4))return 1;
            


        }
        return 0;
    }
    if(x1>x2)swap(x1, x2);
    if(x3>x4)swap(x3, x4);
    if(y1>y2)swap(y1, y2);
    if(y3>y4)swap(y3, y4);

    if(x1<=c_x&&c_x<=x2&&x3<=c_x&&c_x<=x4){
        if(y1<=c_y&&c_y<=y2&&y3<=c_y&&c_y<=y4)return 1;
    }
    return 0;
    
}



//-------------------------------------------------------------------------------------
/*点(p_x,p_y)と線分(x1,y1)(x2,y2)の距離*/
/* straight_line_passing_through_the_two_points */
/* vertical_straight_line */
/* intersection_and_parallel */
double distance_between_the_line_segment_and_point(double p_x, double p_y, double x1, double y1, double x2, double y2){
    double a,b,c;//(x1,y1)(x2,y2)を通る直線
    straight_line_passing_through_the_two_points(x1, y1, x2, y2, &a, &b, &c);
    double a2,b2,c2;//直線ax+by+c=0に垂直な直線
    double v_x, v_y;//二つの直線の交点
    bool is_not_parallel = vertical_straight_line(p_x, p_y, a, b, c, &a2, &b2, &c2);
    intersection_and_parallel(a, b, c, a2, b2, c2, &v_x, &v_y);
    
    if(is_not_parallel){
        if(min(x1,x2)<=v_x&&v_x<=max(x1,x2)&&min(y1,y2)<=v_y&&v_y<=max(y1,y2)){//(v_x,v_y)は線分の中にある
            //cout<<"a ";
            return distance_between_the_two_pointss(p_x, p_y, v_x, v_y);
        }else{//(v_x,v_y)は線分の外にある
            double l1 = distance_between_the_two_pointss(p_x, p_y, x1, y1);
            double l2 = distance_between_the_two_pointss(p_x, p_y, x2, y2);
            //cout<<"b ";
            return min(l1,l2);
        }
    }else{
        if(min(x1,x2)<=p_x&&p_x<=max(x1,x2)&&min(y1,y2)<=p_y&&p_y<=max(y1,y2)){//(p_x,p_y)は線分の中にある
            //cout<<"c ";
            return 0.0;
        }else{//(p_x,p_y)は線分の外にある
            double l1 = distance_between_the_two_pointss(p_x, p_y, x1, y1);
            double l2 = distance_between_the_two_pointss(p_x, p_y, x2, y2);
            //cout<<"d ";
            return min(l1,l2);
        }
    }
    
}


//-------------------------------------------------------------------------------------
/*線分(x1,y1)(x2,y2)と線分(x3,y3)(x4,y4)の距離*/

double distance_between_two_line_segment(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4){
    //同一直線上にある場合
    
    //ぶつかる場合
    if(line_segment_cross_decision(x1, y1, x2, y2, x3, y3, x4, y4)){
        return 0.0;
    }
    
    //ぶつからない場合
    double l1,l2,l3,l4;
    l1=distance_between_the_line_segment_and_point(x1, y1, x3, y3, x4, y4);
    //cout<<l1<<endl;
    l2=distance_between_the_line_segment_and_point(x2, y2, x3, y3, x4, y4);
    //cout<<l2<<endl;

    l3=distance_between_the_line_segment_and_point(x3, y3, x1, y1, x2, y2);
    //cout<<l3<<endl;
    l4=distance_between_the_line_segment_and_point(x4, y4, x1, y1, x2, y2);
    
        
    //cout<<l4<<endl;
    
    return min(l1,min(l2,min(l3,l4)));
}



int main(int argc, const char * argv[]){
    int q;
    cin>>q;
    for(int i=0; i<q; i++){
        double x1,x2,x3,x4,y1,y2,y3,y4;
        cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
        

        cout<< fixed << setprecision(10)<<distance_between_two_line_segment(x1, y1, x2, y2, x3, y3, x4, y4)<<endl;
        //cout<<"========"<<endl;
        
        
        
    }
}

Measured memory_bytes: 1296