#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>

#define INF 10000

int t_rail[101][101],c_rail[101][101];
int ans[101],used[101];

using namespace std;
int main(void)
{
   int m,n;
   int a,b,cost,time;
   int call,s,g,r;
   while(scanf("%d%d",&m,&n),m+n)
   {
      for(int i = 0;i < 101;i++)
         for(int j = 0;j < 101;j++)
            {
               t_rail[i][j] = INF;
               c_rail[i][j] = INF;
            }

      for(int i = 0;i < m;i++)
      {
         scanf("%d%d%d%d",&a,&b,&cost,&time);
         t_rail[a][b] = time;
         t_rail[b][a] = time;
         c_rail[a][b] = cost;
         c_rail[b][a] = cost;
      }

      scanf("%d",&call);
      for(int j = 0;j < call;j++)
      {
         scanf("%d%d%d",&s,&g,&r);
         fill(ans,ans+101,INF);
         fill(used,used+101,0);
         ans[s] = 0;
         if(r == 0)
         {
            while(1)
            {
               int v = -1;
               for(int k = 0;k < 101;k++) if(used[k] == 0 && (v == -1 || ans[v] > ans[k]))v = k;
               if(v == -1) break;
               used[v] = 1;
               for(int k = 0;k < 101;k++) ans[k] = min(ans[k],ans[v]+c_rail[v][k]);
            }
         }
         else
         {
            while(1)
            {
               int v = -1;
               for(int k = 0;k < 101;k++) if(used[k] == 0 && (v == -1 || ans[v] > ans[k])) v = k;
               if(v == -1) break;
               used[v] = 1;
               for(int k = 0;k < 101;k++) ans[k] = min(ans[k],ans[v]+t_rail[v][k]);
            }
         }
         printf("%d\n",ans[g]);
      }
   }
   return 0;
}