#include <bits/stdc++.h>
using namespace std;

#define dump(...) cout<<"# "<<#__VA_ARGS__<<'='<<(__VA_ARGS__)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}

template<typename Tuple>
void print_tuple(ostream&,const Tuple&){}
template<typename Car,typename... Cdr,typename Tuple>
void print_tuple(ostream& os,const Tuple& t){
	print_tuple<Cdr...>(os,t);
	os<<(sizeof...(Cdr)?",":"")<<get<sizeof...(Cdr)>(t);
}
template<typename... Args>
ostream& operator<<(ostream& os,const tuple<Args...>& t){
	print_tuple<Args...>(os<<'(',t);
	return os<<')';
}

template<typename Ch,typename Tr,typename C>
basic_ostream<Ch,Tr>& operator<<(basic_ostream<Ch,Tr>& os,const C& c){
	os<<'[';
	for(auto i=begin(c);i!=end(c);++i)
		os<<(i==begin(c)?"":" ")<<*i;
	return os<<']';
}

constexpr int INF=1e9;
constexpr int MOD=1e9+7;
constexpr double EPS=1e-9;

struct Point{
	int x,y,z;
	Point():x(0),y(0),z(0){}
	Point(int x,int y,int z):x(x),y(y),z(z){}
};
ostream& operator<<(ostream& os,const Point& p){
	return os<<'('<<p.x<<','<<p.y<<','<<p.z<<')';
}
double Distance(Point a,Point b){
	double x=a.x-b.x,y=a.y-b.y,z=a.z-b.z;
	return sqrt(x*x+y*y+z*z);
}

template<typename T> void chmin(T& a,const T& b){a=min(a,b);}
template<typename T> void chmax(T& a,const T& b){a=max(a,b);}

int main()
{
	#ifndef _GLIBCXX_DEBUG
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	constexpr char endl='\n';
	#endif
	
	for(int n,q;cin>>n>>q && n|q;){
		vector<Point> warps(n); // warp
		for(auto& p:warps) cin>>p.x>>p.y>>p.z;
		vector<vector<Point>> paths(q); // path
		rep(i,q){
			int m; cin>>m;
			paths[i].resize(m);
			for(auto& p:paths[i]) cin>>p.x>>p.y>>p.z;
		}
		
		Point offset(INF,INF,INF);
		for(auto p:warps)
			chmin(offset.x,p.x),chmin(offset.y,p.y),chmin(offset.z,p.z);
		rep(i,q) for(auto p:paths[i])
			chmin(offset.x,p.x),chmin(offset.y,p.y),chmin(offset.z,p.z);
		for(auto& p:warps)
			p.x-=offset.x,p.y-=offset.x,p.z-=offset.z;
		rep(i,q) for(auto& p:paths[i])
			p.x-=offset.x,p.y-=offset.x,p.z-=offset.z;
		
		const int c=cbrt(n/10.+1); // 分割数．1区画あたり10個くらいの点があるように分割する．
		Point size(0,0,0); // 1区画あたりの大きさ
		for(auto p:warps)
			chmax(size.x,p.x),chmax(size.y,p.y),chmax(size.z,p.z);
		rep(i,q) for(auto p:paths[i])
			chmax(size.x,p.x),chmax(size.y,p.y),chmax(size.z,p.z);
		size.x=(size.x+c)/c,size.y=(size.y+c)/c,size.z=(size.z+c)/c;
		
		typedef vector<Point> vp;
		typedef vector<vp> vvp;
		typedef vector<vvp> vvvp;
		typedef vector<vvvp> vvvvp;
		vvvvp grid(c,vvvp(c,vvp(c)));
		for(auto p:warps)
			grid[p.x/size.x][p.y/size.y][p.z/size.z].push_back(p);
		
		for(auto path:paths){
			vd nears;
			for(auto p:path){
				double mn=INF;
				int ix=p.x/size.x,iy=p.y/size.y,iz=p.z/size.z;
				repi(i,max(ix-1,0),min(ix+2,c))
				repi(j,max(iy-1,0),min(iy+2,c))
				repi(k,max(iz-1,0),min(iz+2,c))
				for(auto q:grid[i][j][k])
					mn=min(mn,Distance(p,q));
				nears.push_back(mn);
			}
			double sum=0;
			rep(i,path.size()-1)
				sum+=min(Distance(path[i],path[i+1]),nears[i]+nears[i+1]);
			printf("%.9f\n",sum);
		}
	}
}

Measured memory_bytes: 5896