#include <bits/stdc++.h>
using namespace std;
class point
{
public:
int x, y, z;
point(int x = 0, int y = 0, int z = 0): x(x), y(y), z(z)
{
}
point operator - (const point &b) const
{
return point(x - b.x, y - b.y, z - b.z);
}
int operator * (const point &b) const
{
return x * b.x + y * b.y + z * b.z;
}
int sqr()
{
return *this * *this;
}
double len()
{
return sqrt(sqr());
}
};
long long sqr(int x)
{
return (long long) x * x;
}
int main()
{
cout.setf(ios::fixed);
cout.precision(10);
int tt;
cin >> tt;
while (tt--)
{
point o, a, b;
int r;
cin >> o.x >> o.y >> o.z >> r;
cin >> a.x >> a.y >> a.z;
cin >> b.x >> b.y >> b.z;
if ((a - b).sqr() == 0 || (o - a) * (b - a) <= 0 || (o - b) * (a - b) <= 0)
{
cout << (a - b).len() << "
";
}
else
{
double h = sqrt((o - a).sqr() - (double) sqr((o - a) * (b - a)) / (a - b).sqr());
if (r <= h)
{
cout << (a - b).len() << "
";
}
else
{
double go_a = sqrt((o - a).sqr() - sqr(r));
double go_b = sqrt((o - b).sqr() - sqr(r));
double angle = atan2(sqrt((o - a).sqr() - h * h), h) + atan2(sqrt((o - b).sqr() - h * h), h) - atan2(go_a, r) - atan2(go_b, r);
cout << angle * r + go_a + go_b << "
";
}
}
}
return 0;
}