#include<iostream>
#include<vector>
#include<cstring>
#define INF 30100
#define rep(i,n) for(int i=0;i<n;i++)
#define foreach(r,p,it) for(r::iteartor it=p.begin();it!=p.end();++it)
using namespace std;
struct E
{
    unsigned int a,b,c;
    E(){};
    E(int a, int b,int c):a(a),b(b),c(c){};
};
E cs[610];
E ts[610];

unsigned int d[110];
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        if(n==0&&m==0)break;
        
        int a,b,c,t;
        rep(i,n)
        {
            cin>>a>>b>>c>>t;
            cs[i*2] = E(a,b,c);
            cs[i*2+1] = E(b,a,c);
            
            ts[i*2] = E(a,b,t);
            ts[i*2+1] = E(b,a,t);

        }
        int p,q,r,k;
        cin >> k;
        rep(i,k)
        {
            cin >> p >> q >> r;
            memset(d,INF,sizeof(d));
            d[p] = 0;
            bool update = false;
            if(r == 0)
            {
                while(1)
                {
                    update = false;
                    rep(j,n*2)
                    {
                        if(d[ cs[j].b] != INF && d[cs[j].b] > d[cs[j].a] + cs[j].c )
                        {
                            d[cs[j].b] =  d[cs[j].a] + cs[j].c;
                            update = true;
                        }


                    }
                    if(!update) break;
                }
                cout << d[q] << endl;
            }
            else
            {
                while(1)
                {
                    update = false;
                    rep(j,n*2)
                    {
                        if(d[ ts[j].b] != INF && d[ts[j].b] > d[ts[j].a] + ts[j].c)
                        {
                            d[ts[j].b] =  d[ts[j].a] + ts[j].c;
                            update = true;
                        }
                    }
                    if(!update) break;
                }
                cout << d[q] << endl;
            }
        }
    }
    return 0;
}