/*
  [BAPC'07] HIKING
  by: Jan Kuipers
*/

using namespace std;

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>

const double eps = 1e-9;
const double inf = 1e100;

struct point {
  double x,y;
  point() {}
  point(double _x, double _y): x(_x), y(_y) {}
};

point operator + (point a, point b) { return point(a.x+b.x, a.y+b.y); }
point operator - (point a, point b) { return point(a.x-b.x, a.y-b.y); }
point operator * (double a, point b) { return point(a*b.x, a*b.y); }
double operator * (point a, point b) { return a.x*b.x + a.y*b.y; }
point normal (point a) { return point(a.y, -a.x); }

int main () {

  int runs;
  cin>>runs;
  while (runs--) {

    int D,N;
    cin>>D>>N;

    point p1,p2;
    cin>>p1.x>>p1.y;
    cin>>p2.x>>p2.y;
    
    vector<point> t(N);
    for (int i=0; i<N; i++)
      cin>>t[i].x>>t[i].y;

    int ok=0;
    for (int i=0; i<N; i++)
      if ((p1-t[i])*(p1-t[i]) <= D*D) ok=1;

    if (!ok) {
      cout<<-1<<endl;
      continue;
    }
    
    vector<point> p;
    p.push_back(p1);
    p.push_back(p2);

    for (int i=0; i<N; i++)
      for (int j=0; j<i; j++) {
	double diff = (t[i]-t[j])*(t[i]-t[j]);
	if (diff <= 4*D*D) {
	  double s = sqrt((4*D*D-diff) / (4*diff));
	  point n = normal(t[i]-t[j]);
	  
	  p.push_back(0.5*(t[i]+t[j]) + s*n);
	  p.push_back(0.5*(t[i]+t[j]) - s*n);
	}
      }

    int idx=2;
    for (int i=2; i<p.size(); i++) {
      ok=1;
      for (int j=0; j<N; j++)
	if ((p[i]-t[j])*(p[i]-t[j]) < D*D - eps) ok=0;
      if (ok) p[idx++]=p[i];
    }

    p.erase(p.begin()+idx, p.end());

    int P=p.size();
    vector<vector<double> > d(P, vector<double>(P, inf));

    for (int i=0; i<P; i++)
      for (int j=0; j<P; j++) {
	vector<pair<double,double> > ivals;

	for (int k=0; k<N; k++) {
	  double A = (p[j]-p[i])*(p[j]-p[i]);
	  double B = 2*(p[j]-p[i])*(p[i]-t[k]);
	  double C = (p[i]-t[k])*(p[i]-t[k]) - D*D;
	  
	  if (B*B-4*A*C < 0) continue;
	  
	  double fr = (-B-sqrt(B*B-4*A*C)) /2/A;
	  double to = (-B+sqrt(B*B-4*A*C)) /2/A;
	  
	  if (to<-eps || fr>1+eps) continue;
	  
	  ivals.push_back(make_pair(max(fr,0.0),min(to,1.0)));
	}

	sort(ivals.begin(),ivals.end());
	ok=1;
	double to=0;
	
	for (int k=0; k<ivals.size(); k++) {
	  if (ivals[k].first > to+eps) ok=0;
	  to=max(to, ivals[k].second);
	}
	if (to < 1-eps) ok=0;

	if (ok) d[i][j] = sqrt((p[j]-p[i])*(p[j]-p[i]));
      }

    for (int k=0; k<P; k++)
      for (int i=0; i<P; i++)
	for (int j=0; j<P; j++)
	  d[i][j] = min(d[i][j], d[i][k]+d[k][j]);

    if (d[0][1]==inf)
      cout<<-1<<endl;
    else
      cout<<setprecision(10)<<d[0][1]<<endl;
  }
  
  return 0;
}
