#include <iostream>

#include <vector>

using namespace std;

 

#define INF 200000000

 

int main()

{

    int e, n, k;

    int a, b, cost, time, r;

    while(cin >> e >> n && e && n)

    {

        vector<vector<int> > costs(n+1, vector<int>(n+1, INF)),

                            times(n+1, vector<int>(n+1, INF));

        for(int i=0; i<costs.size(); i++)

        {

            costs[i][i] = 0;

            times[i][i] = 0;

        }

 

        for(int i=0; i<e; i++)

        {

            cin >> a >> b >> cost >> time;

            costs[a][b] = cost;

            costs[b][a] = cost;

            times[a][b] = time;

            times[b][a] = time;

        }

 

        cin >> k;

        for(int q=0; q<k; q++)

        {

            cin >> a >> b >> r;

            vector<vector<int> > v;

            if(r == 0) v = costs;

            else v = times;

 

            vector<int> distance(n+1, INF); distance[a] = 0;

            vector<int> reached(n+1, 0);

 

            while(true)

            {

                int im = -1;

                for(int i=0; i<reached.size(); i++) if(reached[i] == 0)

                {

                    if(im == -1 || distance[i] < distance[im]) im = i;

                }

                if(im == b) { cout << distance[im] << endl; break; }

 

                reached[im] = 1;

               for(int j=0; j<v[im].size(); j++)

                {

                    if(distance[im] + v[im][j] < distance[j])

                        distance[j] = distance[im] + v[im][j];

                }

            }

        }

    }

    return 0;
}