#include<bits/stdc++.h>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define EPS (1e-8)
#define equals(a,b) (fabs((a)-(b))<EPS)

using namespace std;

typedef pair<int,int> ii;

bool LT(double a,double b) { return !equals(a,b) && a < b ; }

struct Edge {
  int to,cap;
  double cost;
  int rev;
};

const int MAX_V = 1000, IINF = INT_MAX;
int V;
vector<Edge> G[MAX_V];

double h[MAX_V],dist[MAX_V];//////// fpwjefoajfpojeawfopefa
int prevv[MAX_V], preve[MAX_V];


inline void add_edge(int from,int to,int cap,double cost){
  G[from].push_back((Edge){to,cap,cost,G[to].size()});
  G[to].push_back((Edge){from,0,-cost,G[from].size()-1});
}

double min_cost_flow(int s,int t,int f){
  double res = 0;
  fill(h,h+V,0);
  while(LT(0,f)){
    priority_queue<ii,vector<ii>,greater<ii> > Q;
    fill(dist,dist+V,INT_MAX);
    dist[s] = 0;
    Q.push(ii(0,s));
    while( !Q.empty() ){
      ii p = Q.top(); Q.pop();
      int v = p.second;
      if( LT(dist[v],p.first) ) continue;
      for(int i=0;i<G[v].size();i++)      {
	Edge &e = G[v][i];
	if( LT(0,e.cap) && LT(dist[v]+e.cost+h[v]-h[e.to],dist[e.to]) ) {
	  dist[e.to] = dist[v]+e.cost+h[v]-h[e.to];
	  prevv[e.to] = v;
	  preve[e.to] = i;
	  Q.push(ii(dist[e.to],e.to));
	}
      }
    }
    if( equals(dist[t],INT_MAX) ) return -1;
    rep(v,V) h[v] += dist[v];
    int d = f;
    for(int v=t;v!=s;v=prevv[v]) d = min(d,G[prevv[v]][preve[v]].cap);
    f -= d;
    res += d * h[t];
    for(int v=t;v!=s;v=prevv[v]) {
      Edge &e = G[prevv[v]][preve[v]];
      e.cap -= d;
      G[v][e.rev].cap += d;
    }
  }
  return res;
}

typedef vector<double> vd;
typedef vector<vd> mat;



vector<double> f(const mat& A,const vd& b){
  int n = A.size();
  mat B(n,vd(n+1));
  rep(i,n) rep(j,n) B[i][j] = A[i][j];
  rep(i,n) B[i][n] = b[i];

  rep(i,n){
    int pivot = i;
    REP(j,i,n) if( LT(abs(B[pivot][i]),abs(B[j][i])) ) pivot = j;
    swap(B[i],B[pivot]);
    if( abs(B[i][i]) < EPS ) return vd();
    REP(j,i+1,n+1) B[i][j] /= B[i][i];
    rep(j,n){
      if( i != j ){
	REP(k,i+1,n+1) B[j][k] -= B[j][i] * B[i][k];
      }
    }
  }  

  vd x(n);
  rep(i,n) x[i] = B[i][n];
  return x;
}

void init(int size=MAX_V){
  rep(i,size) G[i].clear();
}

int main(){
  int T;
  cin >> T;
  while( T-- ){
    int N,s,t,F;
    cin >> N >> s >> t#include<bits/stdc++.h>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define EPS (1e-8)
#define equals(a,b) (fabs((a)-(b))<EPS)

using namespace std;

typedef pair<int,int> ii;

bool LT(double a,double b) { return !equals(a,b) && a < b ; }

struct Edge {
  int to,cap;
  double cost;
  int rev;
};

const int MAX_V = 1000, IINF = INT_MAX;
int V;
vector<Edge> G[MAX_V];

double h[MAX_V],dist[MAX_V];//////// fpwjefoajfpojeawfopefa
int prevv[MAX_V], preve[MAX_V];


inline void add_edge(int from,int to,int cap,double cost){
  G[from].push_back((Edge){to,cap,cost,G[to].size()});
  G[to].push_back((Edge){from,0,-cost,G[from].size()-1});
}

double min_cost_flow(int s,int t,int f){
  double res = 0;
  fill(h,h+V,0);
  while(LT(0,f)){
    priority_queue<ii,vector<ii>,greater<ii> > Q;
    fill(dist,dist+V,INT_MAX);
    dist[s] = 0;
    Q.push(ii(0,s));
    while( !Q.empty() ){
      ii p = Q.top(); Q.pop();
      int v = p.second;
      if( LT(dist[v],p.first) ) continue;
      for(int i=0;i<G[v].size();i++)      {
	Edge &e = G[v][i];
	if( LT(0,e.cap) && LT(dist[v]+e.cost+h[v]-h[e.to],dist[e.to]) ) {
	  dist[e.to] = dist[v]+e.cost+h[v]-h[e.to];
	  prevv[e.to] = v;
	  preve[e.to] = i;
	  Q.push(ii(dist[e.to],e.to));
	}
      }
    }
    if( equals(dist[t],INT_MAX) ) return -1;
    rep(v,V) h[v] += dist[v];
    int d = f;
    for(int v=t;v!=s;v=prevv[v]) d = min(d,G[prevv[v]][preve[v]].cap);
    f -= d;
    res += d * h[t];
    for(int v=t;v!=s;v=prevv[v]) {
      Edge &e = G[prevv[v]][preve[v]];
      e.cap -= d;
      G[v][e.rev].cap += d;
    }
  }
  return res;
}

typedef vector<double> vd;
typedef vector<vd> mat;



vector<double> f(const mat& A,const vd& b){
  int n = A.size();
  mat B(n,vd(n+1));
  rep(i,n) rep(j,n) B[i][j] = A[i][j];
  rep(i,n) B[i][n] = b[i];

  rep(i,n){
    int pivot = i;
    REP(j,i,n) if( LT(abs(B[pivot][i]),abs(B[j][i])) ) pivot = j;
    swap(B[i],B[pivot]);
    if( abs(B[i][i]) < EPS ) return vd();
    REP(j,i+1,n+1) B[i][j] /= B[i][i];
    rep(j,n){
      if( i != j ){
	REP(k,i+1,n+1) B[j][k] -= B[j][i] * B[i][k];
      }
    }
  }  

  vd x(n);
  rep(i,n) x[i] = B[i][n];
  return x;
}

void init(int size=MAX_V){
  rep(i,size) G[i].clear();
}

int main(){
  int T;
  cin >> T;
  while( T-- ){
    int N,s,t,F;
    cin >> N >> s >> t >> F;
    mat A(N,vector<double>(N,0));
    vector<double> b(N,0);
    rep(i,N) {
      rep(j,N) {
	cin >> A[i][j];
      }
      cin >> b[i];
    }
    
    vector<double> ans = f(A,b);
    
    init();

    rep(i,N) {
      int M;
      cin >> M;
      vector<ii> vec(M);
      rep(j,M) cin >> vec[j].first;
      rep(j,M) cin >> vec[j].second;
      rep(j,M) {
	add_edge(i,vec[j].first,vec[j].second,abs(ans[i]-ans[vec[j].first]));
      }
    }

    V = N;
    double temp = min_cost_flow(s,t,F);
    if( equals(temp,-1) ) puts("impossible");
    else printf("%.10f\n",temp);

  }
  return 0;
}
 >> F;
    mat A(N,vector<double>(N,0));
    vector<double> b(N,0);
    rep(i,N) {
      rep(j,N) {
	cin >> A[i][j];
      }
      cin >> b[i];
    }
    
    vector<double> ans = f(A,b);
    
    init();

    rep(i,N) {
      int M;
      cin >> M;
      vector<ii> vec(M);
      rep(j,M) cin >> vec[j].first;
      rep(j,M) cin >> vec[j].second;
      rep(j,M) {
	add_edge(i,vec[j].first,vec[j].second,abs(ans[i]-ans[vec[j].first]));
      }
    }

    V = N;
    double temp = min_cost_flow(s,t,F);
    if( equals(temp,-1) ) puts("impossible");
    else printf("%.10f\n",temp);

  }
  return 0;
}