#include <iostream>
#include <algorithm>
#include <queue>
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=(int)1e9, MAXN=128;
int C[2][MAXN][MAXN], cost[MAXN], N, M;

void dk(int p,int q,int r)
{
        priority_queue<pii,vector<pii>,greater<pii> > Q;
        Q.push(pii(0,p)); cost[p]=0;

        while(!Q.empty())
        {
                const int cc = Q.top().first, pp = Q.top().second; Q.pop();
                if(pp==q){ break; }

                rep(i,M) if(cost[pp]+C[r][pp][i]<cost[i]){ cost[i]=C[r][pp][i]+cost[pp]; Q.push(pii(cc+C[r][pp][i],i)); }
        }
}

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(cost,cost+N,INF); cin>>a>>b>>c; dk(a-1,b-1,c); cout<<cost[b-1]<<endl; }
        return 0;
}