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 C# (judge verdict: Wrong Answer)

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
//using System.Numerics;
using Enu = System.Linq.Enumerable;

public class Program
{
    public void Solve()
    {
        int NQ = Reader.Int();

        for (int q = 0; q < NQ; q++)
        {
            var a = new Point(Reader.Int(), Reader.Int());
            var b = new Point(Reader.Int(), Reader.Int());
            var c = new Point(Reader.Int(), Reader.Int());
            var d = new Point(Reader.Int(), Reader.Int());
            var seg1 = new Segment(a, b);
            var seg2 = new Segment(c, d);
            Console.WriteLine(Distance(seg1, seg2));
        }
        Console.ReadLine();
    }

    public double Distance(Point p, Segment seg)
    {
        return Math.Min(p.Distance(seg.A), p.Distance(seg.B));
    }

    public double Distance(Segment seg1, Segment seg2)
    {
        if (SegmentIntersect(seg1, seg2)) return 0;
        return Math.Min(Math.Min(Distance(seg1.A, seg2), Distance(seg1.B, seg2)),
                        Math.Min(Distance(seg2.A, seg1), Distance(seg2.B, seg1)));
    }

    bool SegmentIntersect(Segment seg1, Segment seg2)
    {
        var c1 = (seg1.B - seg1.A).Det(seg2.A - seg1.A);
        if (c1 == 0 && (seg1.A - seg2.A).Dot(seg1.B - seg2.A) <= 0) return true;
        var c2 = (seg1.B - seg1.A).Det(seg2.B - seg1.A);
        if (c2 == 0 && (seg1.A - seg2.B).Dot(seg1.B - seg2.B) <= 0) return true;
        var c3 = (seg2.B - seg2.A).Det(seg1.A - seg2.A);
        if (c3 == 0 && (seg2.A - seg1.A).Dot(seg2.B - seg1.A) <= 0) return true;
        var c4 = (seg2.B - seg2.A).Det(seg1.B - seg2.A);
        if (c4 == 0 && (seg2.A - seg1.B).Dot(seg2.B - seg1.B) <= 0) return true;
        return c1 * c2 < 0 && c3 * c4 < 0;
    }

    public class Segment
    {
        public Point A, B;
        public Segment(Point a, Point b) { A = a; B = b; }
    }

    public struct Point : IEquatable<Point>, IComparable<Point>
    {
        public static readonly double Eps = 1e-10;
        public double X, Y;

        public Point(double x, double y) { X = x; Y = y; }

        public static Point operator +(Point a, Point b)
        {
            return new Point(Add(a.X, b.X), Add(a.Y, b.Y));
        }
        public static Point operator -(Point a, Point b)
        {
            return new Point(Add(a.X, -b.X), Add(a.Y, -b.Y));
        }
        public static Point operator *(Point p, double d)
        {
            return new Point(p.X * d, p.Y * d);
        }
        public double Distance(Point b) { return Math.Sqrt((b - this).Dot(b - this)); }
        public double Dot(Point b) { return Add(X * b.X, Y * b.Y); }
        public double Det(Point b) { return Add(X * b.Y, -Y * b.X); }
        public bool Equals(Point b) { return X == b.X && Y == b.Y; }
        public int CompareTo(Point b) { return X != b.X ? Math.Sign(X - b.X) : Math.Sign(Y - b.Y); }
        public override int GetHashCode() { return (int)(X * 100000 + Y); }
        public override string ToString() { return X + ", " + Y; }

        private static double Add(double a, double b)
        {
            if (Math.Abs(a + b) < Eps * (Math.Abs(a) + Math.Abs(b))) return 0;
            return a + b;
        }
    }
}


class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    private static TextReader reader = Console.In;
    private static readonly char[] separator = { ' ' };
    private static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    private static string[] A = new string[0];
    private static int i;
    private static void Init() { A = new string[0]; }
    public static void Set(TextReader r) { reader = r; Init(); }
    public static void Set(string file) { reader = new StreamReader(file); Init(); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Enu.Range(0, N).Select(i => Int()).ToArray(); }
    public static int[][] IntTable(int H) { return Enu.Range(0, H).Select(i => IntLine()).ToArray(); }
    public static string[] StringArray(int N) { return Enu.Range(0, N).Select(i => Next()).ToArray(); }
    public static string Line() { return reader.ReadLine().Trim(); }
    private static string[] Split(string s) { return s.Split(separator, op); }
    private static string Next() { CheckNext(); return A[i++]; }
    private static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}