#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define sz(c) (int) c.size()
#define all(c) c.begin(), c.end()
#define tr(container,it) for(__typeof(container.begin()) it = container.begin();it != container.end(); it++)
#define present(s,x) s.find(x) != s.end()
#define cpresent(c,x) find(all(c),x) != c.end()
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define rep(i,n) for(int i=0;i<n;i++)
#define dep(i,n) for(int i=n-1;i>=0;i--)
#define repn(i,a,b) for(int i=a; i<b;i++)
#define ini(n) scanf("%d",&n)
#define ind(n,m) scanf("%d %d",&n,&m);
#define ins(n) scanf("%s",n);
#define opi(n) printf("%d",n)
#define ops(n) printf("%s",n)
#define opn printf("\n")
#define opsp printf(" ");
#define opa(n) cout<<n
typedef pair<int,int> pi;
typedef vector<pi> vp;
typedef vector<vp> vvp;
typedef vector<int> vi;
typedef unsigned long long int ull;
typedef long long int ll;
typedef vector<ll> vl;
typedef vector< vi > vvi;
typedef priority_queue<int> pq;
typedef priority_queue<int, vi , greater<int> >minpq;
typedef priority_queue<pi,vp,greater<pi> > mpq;
pair<ll,ll> MinRisk(vector<int>&Marked , int idx , int N , vvi tim , vvi risk , int T , pair<ll,ll> dp[300][300] ){
//~ trace3("inside state " , idx, T);
//~ if(T < 0){
//~ cout<<"current Time is less than 0 so can not be possible return INT_MAX\n" ;
//~ return mp(INT_MAX,INT_MAX);
//~ }
if(idx == N-1){
//~ cout<<"current state is goal state thats why returnign 0 \n" ;
return {0,0} ;
}
if(dp[idx][T].ff != -1)
return dp[idx][T] ;
ll cost = INT_MAX ;
ll money= INT_MAX ;
for(int i= 0 ; i<N ; i ++){
if(i == idx or Marked[i] == 1 or T<tim[idx][i])
continue;
Marked[i] = 1;
//~ cout<<"state ("<<idx<<", "<<T<<") is "<<endl;
//~ cout<<"calling State ( "<<i<<","<<T-tim[idx][i]<<")"<<endl;
pair<ll,ll> calc = MinRisk(Marked, i ,N, tim ,risk , T-tim[idx][i] , dp);
ll tmp = risk[idx][i] + calc.ff;
ll tmp2 = tim[idx][i] + calc.ss;
//~ trace1(tmp);
if(tmp <= cost){
cost = tmp ;
money = tmp==cost ? min(money,tmp2) : tmp2;
}
Marked[i] = 0 ;
}
Marked[idx] = 1;
//~ cout<<"State ( "<<idx<<","<<T<<")"<<"returned "<<"optimal cost as "<<cost<<"and optimal time as "<<money<<endl;
dp[idx][T] = {cost,money};
return {cost,money};
}
int main(){
//ios_base::sync_with_stdio(false);cin.tie(NULL);
int t;
cin>>t;
while(t--){
int N,T;
cin>>N>>T;
vvi risk;
vvi tim ;
//~ trace2(N,T);
risk.resize(N,vi(N,0));
tim.resize(N,vi(N,0));
vector<int>Marked(N,0);
for(int i = 0 ; i < N ; i++){
for(int j=0; j < N; j++){
cin>>tim[i][j] ;
}
}
for(int i = 0 ; i < N ; i++){
for(int j=0; j < N; j++){
cin>>risk[i][j] ;
}
}
pair<ll,ll> dp[300][300] ;
for(int i=0; i<300 ; i++){
for(int j = 0 ; j < 300 ; j++){
dp[i][j].ff = -1;
dp[i][j].ss = -1;
}
}
//~ cout<<"calling State ( "<<0<<","<<T<<")"<<endl;
pair<ll,ll> ans= MinRisk(Marked,0,N,tim, risk , T , dp );
if(ans.ff== INT_MAX)
cout<<"-1"<<endl;
else{
cout<<ans.ff<<" "<<ans.ss<<endl;
}
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKI2RlZmluZSBtb2QgMTAwMDAwMDAwNwojZGVmaW5lIHN6KGMpICAoaW50KSBjLnNpemUoKQojZGVmaW5lIGFsbChjKSBjLmJlZ2luKCksIGMuZW5kKCkKI2RlZmluZSB0cihjb250YWluZXIsaXQpIGZvcihfX3R5cGVvZihjb250YWluZXIuYmVnaW4oKSkgaXQgPSBjb250YWluZXIuYmVnaW4oKTtpdCAhPSBjb250YWluZXIuZW5kKCk7IGl0KyspCiNkZWZpbmUgcHJlc2VudChzLHgpIHMuZmluZCh4KSAhPSBzLmVuZCg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