#include <iostream>
using namespace std;

#define REP(i,p,n) for(int i=p;i<n;++i)
#define rep(i,n) REP(i,0,n)

typedef pair<int,int> pii;
const int INF=1e9, MAXM=128;
int C[2][MAXM][MAXM], dist[MAXM], visited[MAXM], N, M;

void dk(int p,int q,int r)
{
        dist[p]=0;

        while(true)
        {
                int from=-1,c=INF;
                rep(i,M) if(!visited[i]&&dist[i]<=c){ c=dist[i]; from=i; }
                if(from==q){ cout<<dist[from]<<endl; return; } else { visited[from]=true; }

                rep(to,M) if(dist[from]+C[r][from][to]<dist[to]){ dist[to]=dist[from]+C[r][from][to]; }
        }
}

int main()
{
        int a,b,c,d;
        cin>>N>>M;
        rep(i,M) rep(j,N) C[0][i][j]=C[1][i][j]=(i==j?0:INF);
        rep(i,N){ cin>>a>>b>>c>>d; --a; --b; C[0][a][b]=C[0][b][a]=c; C[1][a][b]=C[1][b][a]=d; }

        cin>>d;
        rep(i,d){ fill(dist,dist+M,INF); fill(visited,visited+M,false); cin>>a>>b>>c; dk(a-1,b-1,c); }
        return 0;
}