// Arup Guha
// 7/17/2025
// Solution to 2025 SI@UCF Competitive Programming Camp Contest #3 Problem: Frog on a Log

import java.util.*;

public class frogonalog {

	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
		
		// Process cases.
		for (int loop=0; loop<nC; loop++) {
		
			// This is me.
			int x = stdin.nextInt();
			int y = stdin.nextInt();
			pt2D me = new pt2D(x, y);
			
			// Get all line segments.
			int n = stdin.nextInt();
			line[] segs = new line[n];
			for (int i=0; i<n; i++) {
				int x1 = stdin.nextInt();
				int y1 = stdin.nextInt();
				int x2 = stdin.nextInt();
				int y2 = stdin.nextInt();
				pt2D a = new pt2D(x1, y1);
				pt2D b = new pt2D(x2, y2);
				segs[i] = new line(a, b);
			}
			
			// Just get the shortest line segment distance.
			double res = segs[0].distToSeg(me);
			for (int i=1; i<n; i++)
				res = Math.min(res, segs[i].distToSeg(me));
			System.out.printf("%.10f\n",res);
		}
	}
}

/*** Code from lecture - Edited for this problem ***/

class vect2D {

    public double x;
    public double y;

    public vect2D(double myx, double myy) {
        x = myx;
        y = myy;
    }

    public vect2D(pt2D start, pt2D end) {
        x = end.x - start.x;
        y = end.y - start.y;
    }
	
	public vect2D negate() {
		return new vect2D(-x, -y);
	}

    public double dot(vect2D other) {
        return this.x*other.x + this.y*other.y;
    }

    public double mag() {
        return Math.sqrt(x*x+y*y);
    }

    // This formula comes from using the relationship between the dot product and
    // the cosine of the included angle.
    public double angle(vect2D other) {
        return Math.acos(this.dot(other)/mag()/other.mag());
    }

    public double signedCrossMag(vect2D other) {
        return this.x*other.y-other.x*this.y;
    }

    public double crossMag(vect2D other) {
        return Math.abs(signedCrossMag(other));
    }
}

class line {

    final public static double EPSILON = 1e-9;

    public pt2D p;
	public pt2D q;
    public vect2D dir;

    public line(pt2D start, pt2D end) {
        p = start;
		q = end;
        dir = new vect2D(start, end);
    }

    // Returns the shortest distance from other to this line. Sets a vector from the starting
    // point of this line to other and uses the cross product with that vector and the direction
    // vector of the line.
    public double distance(pt2D other) {
        vect2D toPt = new vect2D(p, other);
        return dir.crossMag(toPt)/dir.mag();
    }
	
	/*** This is the key piece of added code. ***/

	// Returns the shortest distance from other to this line segment.
	public double distToSeg(pt2D other) {

		vect2D pToOther = new vect2D(p, other);
		vect2D qToOther = new vect2D(q, other);
		
		// Dot product between vector from start pt to other and direction vector.
		double dot1 = pToOther.dot(dir);
		
		// Dot product between vector from end pt to other and opposite direction vector.
		vect2D negDir = dir.negate();
		double dot2 = qToOther.dot(negDir);
		
		// The shortest line distance is also on the segment.
		// Basically, if this is an acute triangle, just to regular pt to line distance.
		if (dot1 > -EPSILON && dot2 > -EPSILON)
			return this.distance(other);
			
		// Just take the shortest distance to either end point.
		return Math.min(other.dist(p), other.dist(q));
	}

    public static double det(double a, double b, double c, double d) {
        return a*d - b*c;
    }
}

class pt2D {

    public double x;
    public double y;

    public pt2D(double myx, double myy) {
        x = myx;
        y = myy;
    }
	
	public double dist(pt2D other) {
		return Math.sqrt( (x-other.x)*(x-other.x) + (y-other.y)*(y-other.y));
	}
}