#include<iostream>
#define INF 2 << 23
using namespace std;
int main(){
  int n,m;
  while(cin >> n >> m){
    if(n==0&&m==0) break;
    int cost[m+1][m+1];
    int time[m+1][m+1];

    for(int i=0;i<=m;i++)
      for(int j=0;j<=m;j++){
        if(i==j)
          cost[i][j] = time[i][j] = 0;
        else
          cost[i][j] = time[i][j] = INF;
      }

    for(int i=0;i<n;i++){
      int a,b;
      cin >> a >> b;
      cin >> cost[a][b] >> time[a][b];
      cost[b][a] = cost[a][b]; time[b][a] = time[a][b];
    }

    for (int i = 1; i <= m; i++)      // ?????±????????????
      for (int j = 1; j <= m; j++)    // ????§???????
        for (int k = 1; k <= m; k++){  // ??????
          cost[j][k] = min(cost[j][k], cost[j][i] + cost[i][k]);
          time[j][k] = min(time[j][k], time[j][i] + time[i][k]);
        }

    int k;
    cin >> k;
    int p,q,r;
    for(int i=0;i<k;i++){
      cin >> p >> q >> r;
      switch(r){
      case 0: cout << cost[p][q] << endl; break;
      case 1: cout << time[p][q] << endl; break;
      default: break;
      }
    }
  }
}
#include<bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define mp            make_pair
#define pb            push_back
template<class T1, class T2> inline void minup(T1& m, T2 x){ if(m>x) m=static_cast<T2>(x); }
template<class T1, class T2> inline void maxup(T1& m, T2 x){ if(m<x) m=static_cast<T2>(x); }

#define INF 1000000000
#define MOD 1000000009
#define EPS 1E-9

const int MAX_N = 10;
int n;
int a[MAX_N+1][MAX_N+1];

int main()
{
  while((cin >> n) && n){
    rep(i, n+1) rep(j, n+1) a[i][j] = 0;
    rep(i, n)   rep(j, n)   cin >> a[i][j];

    rep(i, n)   rep(j, n)   a[n][j] += a[i][j];
    rep(i, n+1) rep(j, n)   a[i][n] += a[i][j];

    rep(i, n+1) rep(j, n+1) printf("%5d%s", a[i][j], (j==n ? "\n" : ""));
  }

  return 0;
}