#include <stdio.h>
#include <limits.h>
 
int n,m;
 
int Selection(int start,int goal,int Graph[101][101])
{
    int i;
    int min,minp = start;
    int Station[101];
    int Decided[101] = {0,};
 
    for(i=0;i<101;i++)
    {
        Station[i] = INT_MAX;
    }
 
    Station[start] = 0;
 
    while(Decided[goal] == 0)
    {
        for(i=0;i<m+1;i++)
        {
            if(Graph[minp][i] == -1) continue;
 
            if(Station[i] > Graph[minp][i] + Station[minp])
            {
                Station[i] = Graph[minp][i] + Station[minp];
            }
        }
 
        min = INT_MAX;
        for(i=0;i<m+1;i++)
        {
            if(min > Station[i] && Decided[i] != 1)
            {
                min = Station[i];
                minp = i;
            }
        }
        Decided[minp] = 1;
    }
 
    return Station[goal];
}
 
int main()
{
    int Gcost[101][101];
    int Gtime[101][101];
 
    int i,j;
    int a,b,time,cost;
    int k,start,goal,r;
    int ans;
 
    while(1)
    {
        for(i=0;i<101;i++)
        {
            for(j=0;j<101;j++)
            {
                Gcost[i][j] = -1;
                Gtime[i][j] = -1;
            }
        }
 
        scanf("%d%d",&n,&m);
        if(n == 0 && m==0) break;
 
        for(i=0;i<n;i++)
        {
            scanf("%d%d%d%d",&a,&b,&cost,&time);
            Gcost[a][b] = cost;
            Gcost[b][a] = cost;
            Gtime[a][b] = time;
            Gtime[b][a] = time;
        }
 
        scanf("%d",&k);
        for(i=0;i<k;i++)
        {
            scanf("%d%d%d",&start,&goal,&r);
            if(r==0) ans = Selection(start,goal,Gcost);
            else if(r==1) ans = Selection(start,goal,Gtime);
            printf("%d\n",ans);
        }
    }
 
    return 0;
}