#include<stdio.h>
#include<algorithm>
#include <map>
#include<stdio.h>
#include<algorithm>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <iostream>
#include <functional>
#include <time.h>
using namespace std;
clock_t fi, endtime;

const int INF = 100 * 1000 + 2;
const int r_c = 0;
const int r_t = 1;
const int MAX_V = 100;
const int MAX_E = 6000;

int n, m, a, b, c, t;
int k, p, q, r;
int wf[MAX_V][2] = { 0 };

struct edge { int from, to, ct[2]; };
edge edg[MAX_E];

int main()
{
	scanf("%d %d", &n, &m);
	for (int i = 0; i <= m * 2; ++i) {
		scanf("%d %d %d %d", &a, &b, &c, &t);
		edg[i].from = a - 1; edg[i].to = b - 1; edg[i].ct[r_c] = c; edg[i].ct[r_t] = t;
		++i;
		edg[i].from = b - 1; edg[i].to = a - 1; edg[i].ct[r_c] = c; edg[i].ct[r_t] = t;
	}
	scanf("%d", &k);
	for (int i = 0; i < k; ++i) {
		scanf("%d %d %d", &p, &q, &r);
		for (int i = 0; i < n; ++i) {
			wf[i][r] = INF;
		}
		wf[p - 1][r] = 0;
		while (1) {
			bool bo = false;
			for (int i = 0; i < m * 2; ++i) {
				edge e = edg[i];
				if (wf[e.from][r] != INF && wf[e.to][r] > wf[e.from][r] + e.ct[r]) {
					wf[e.to][r] = wf[e.from][r] + e.ct[r];
					bo = true;
				}
			}
			if (!bo) break;
		}
		if (wf[q - 1][r] == INF) printf("INF\n");
		else printf("%d\n", wf[q - 1][r]);
	}
}