#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <numeric>
#include <limits>
#include <functional>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <queue>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
struct Node {
	int u, v, l, a;
	
	bool operator>(const Node& rhs) const { return a > rhs.a; }
};
struct Edge {
	int v, w;
	Edge* next;
};
struct Nde {
	int u, step;
	
	Nde() {}
	
	Nde(int u, int step) : u(u), step(step) {}
	
	bool operator>(const Nde& rhs) const { return step > rhs.step; }
};
const int N = 200010, M = 400010, LGN = 18;
int n, m;
int dis[N], f[N * 2], ht[N * 2], sub[N * 2];
int prt[N * 2][LGN];
Node ed[M];
Edge* g[N];
Edge pool[M * 2];
Edge* ptop;
int find(int x) { return f[x] == x ? f[x] : f[x] = find(f[x]); }
void adde(int u, int v, int w);
void solve();
int main() {
#ifdef LBT
	freopen("test.in", "r", stdin);
	int nol_cl = clock();
#endif
	
	int t;
	scanf("%d", &t);
	while (t--)
		solve();
	
#ifdef LBT
	LOG("Time: %dms\n", int((clock() - nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
	return 0;
}
void solve() {
	memset(g, 0, sizeof(g));
	ptop = pool;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; ++i) {
		scanf("%d%d%d%d", &ed[i].u, &ed[i].v, &ed[i].l, &ed[i].a);
		adde(ed[i].u, ed[i].v, ed[i].l);
		adde(ed[i].v, ed[i].u, ed[i].l);
	}
	sort(ed + 1, ed + m + 1, greater<Node>());
	
	priority_queue<Nde, vector<Nde>, greater<Nde> > heap;
	memset(dis, -1, sizeof(dis));
	heap.push(Nde(1, dis[1] = 0));
	while (!heap.empty()) {
		Nde tmp = heap.top();
		heap.pop();
		if (tmp.step != dis[tmp.u])
			continue;
		for (Edge* p = g[tmp.u]; p; p = p->next)
			if (dis[p->v] == -1 || dis[p->v] > tmp.step + p->w)
				heap.push(Nde(p->v, dis[p->v] = tmp.step + p->w));
	}
	
	iota(f + 1, f + n * 2, 1);
	memcpy(sub, dis, sizeof(dis));
	int cnt = n;
	for (int i = 1; i <= m; ++i) {
		int u = find(ed[i].u), v = find(ed[i].v);
		if (u == v)
			continue;
		f[u] = ++cnt;
		f[v] = cnt;
		prt[u][0] = cnt;
		prt[v][0] = cnt;
		sub[cnt] = min(sub[u], sub[v]);
		ht[cnt] = ed[i].a;
	}
	prt[cnt][0] = 0;
	for (int i = n * 2 - 1; i; --i)
		for (int j = 1; j < LGN; ++j)
			prt[i][j] = prt[prt[i][j - 1]][j - 1];
	
	int q, k, s;
	scanf("%d%d%d", &q, &k, &s);
	int lans = 0;
	while (q--) {
		int v, p;
		scanf("%d%d", &v, &p);
		v = (v + k * (lans % n) - 1) % n + 1;
		p = (p + k * (lans % (s + 1))) % (s + 1);
		for (int i = LGN - 1; i >= 0; --i)
			if (ht[prt[v][i]] > p)
				v = prt[v][i];
		printf("%d\n", lans = sub[v]);
	}
}
void adde(int u, int v, int w) {
	Edge* p = ptop;
	++ptop;
	p->v = v;
	p->w = w;
	p->next = g[u];
	g[u] = p;
	++p;
}
				
				
				| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 | 
| Testcase #1 | 433.56 us | 3 MB + 88 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #2 | 457.25 us | 3 MB + 100 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #3 | 596.74 us | 3 MB + 116 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #4 | 784.17 us | 3 MB + 128 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #5 | 4.608 ms | 3 MB + 544 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #6 | 684.649 ms | 53 MB + 864 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #7 | 3.709 ms | 3 MB + 428 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #8 | 3.713 ms | 3 MB + 432 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #9 | 3.716 ms | 3 MB + 428 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #10 | 530.295 ms | 45 MB + 300 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #11 | 530.268 ms | 45 MB + 304 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #12 | 799.834 ms | 55 MB + 456 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #13 | 799.902 ms | 55 MB + 292 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #14 | 799.959 ms | 54 MB + 724 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #15 | 5.073 ms | 3 MB + 552 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #16 | 5.068 ms | 3 MB + 552 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #17 | 800.608 ms | 54 MB + 992 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #18 | 799.753 ms | 55 MB + 256 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #19 | 1.341 s | 58 MB + 388 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #20 | 1.345 s | 57 MB + 912 KB | Accepted | Score: 5 | 显示更多 |