/* Please see maks.cpp for explanation! */

import java.util.*;
import java.awt.geom.*;

class Range implements Comparable
{
    public final double a, b;

    public Range(double a, double b) {
        this.a = Math.min(a, b);
        this.b = Math.max(a, b);
    }

    public boolean valid()  {
        return a < b && a < 1 && b > 0;
    }

    public int compareTo(Object other) {
        Range r = (Range)other;
        double d = a - r.a;
        if (d == 0)
            d = b - r.b;
        return d < 0 ? -1 : d > 0 ? +1 : 0;
    }
}

public class maks2
{
    static final double eps = 1e-9;
    static final double inf = Double.POSITIVE_INFINITY;

    static double i1, i2;   // intersection points

    static boolean intersect(Point2D c, double r, Point2D v, Point2D dv)
    {
        double dv_len = dv.distance(0.0, 0.0);
        Point2D dc = new Point2D.Double(-dv.getY()/dv_len, dv.getX()/dv_len);
        double det = dc.getY()*dv.getX() - dc.getX()*dv.getY();
        double f = (dc.getX()*(v.getY() - c.getY()) - dc.getY()*(v.getX() - c.getX()))/det;
        double g = (dv.getX()*(v.getY() - c.getY()) - dv.getY()*(v.getX() - c.getX()))/det;
        if(Math.abs(g) > r)
            return false;
        double m = (Math.sqrt(r*r - g*g))/dv_len;
        i1 = f - m;
        i2 = f + m;
        return true;
    }

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int cases = sc.nextInt();
        while (cases-- > 0)
        {
            /* Read input */
            int D = sc.nextInt(), T = sc.nextInt();
            Point2D start = new Point2D.Double(sc.nextInt(), sc.nextInt());;
            Point2D goal  = new Point2D.Double(sc.nextInt(), sc.nextInt());;
            Point2D[] towers = new Point2D.Double[T];
            for (int t = 0; t < T; ++t)
                towers[t] = new Point2D.Double(sc.nextInt(), sc.nextInt());;

            /* Compute intersection points */
            TreeSet<Point2D> pointSet = new TreeSet<Point2D>(
                new Comparator<Point2D>() {
                    public int compare(Point2D p, Point2D q) {
                        double d = p.getX() - q.getX();
                        if (Math.abs(d) < eps)
                        {
                            d = p.getY() - q.getY();
                            if (Math.abs(d) < eps)
                                d = 0;
                        }
                        return d < 0 ? -1 : d > 0 ? +1 : 0;
                    }
                } );
            for(int s = 0; s < T; ++s)
            {
                for(int t = s + 1; t < T; ++t)
                {
                    Point2D v = new Point2D.Double(
                        0.5*(towers[s].getX() + towers[t].getX()),
                        0.5*(towers[s].getY() + towers[t].getY()) );
                    Point2D dv = new Point2D.Double(
                        towers[s].getY() - v.getY(),
                        v.getX() - towers[s].getX() );
                    if(intersect(towers[s], D, v, dv))
                    {
                        pointSet.add(new Point2D.Double(
                            v.getX() + i1*dv.getX(), v.getY() + i1*dv.getY() ));
                        pointSet.add(new Point2D.Double(
                            v.getX() + i2*dv.getX(), v.getY() + i2*dv.getY() ));
                    }
                }
            }

            /* Remove points that lie inside another circle */
            Iterator it = pointSet.iterator();
            while (it.hasNext()) {
                Point2D point = (Point2D)it.next();
                for (Point2D tower : towers)
                    if (tower.distance(point) < D - eps)
                    {
                        it.remove();
                        break;
                    }
            }

            /* Add start and goal points */
            int N = 0;
            Point2D[] points = new Point2D.Double[pointSet.size() + 2];
            for (Point2D point : pointSet)
                points[N++] = point;
            points[N++] = start;
            points[N++] = goal;

            /* Determine permissible edges */
            double[][] dist = new double[N][N];
            for(int i = 0; i < N; ++i)
            {
                for(int j = i + 1; j < N; ++j)
                {
                    /* Determine which parts of the line overlap with a circle */
                    TreeSet<Range> ranges = new TreeSet<Range>();
                    Point2D delta = new Point2D.Double(
                            points[j].getX() - points[i].getX(),
                            points[j].getY() - points[i].getY() );
                    for (Point2D tower: towers)
                    {
                        if (intersect(tower, D, points[i], delta))
                        {
                            Range range = new Range(i1, i2);
                            if (range.valid())
                                ranges.add(range);
                        }
                    }

                    /* Verify that the entire edge has been covered */
                    double last = 0;
                    for (Range range: ranges)
                    {
                        if (last < range.a - eps)
                            break;
                        last = Math.max(last, range.b);
                    }
                    dist[i][j] = dist[j][i] = (last < 1 - eps) ? inf : points[i].distance(points[j]);
                }
            }

            /* Find shortest paths */
            for (int k = 0; k < N; ++k)
                for (int i = 0; i < N; ++i)
                    for (int j = 0; j < N; ++j)
                        dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j]);
            System.out.println(dist[N-2][N-1] < inf ? dist[N-2][N-1] : -1);
        }
    }
}
