#define ri register int
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int N=400005;
inline int re(){
    ri x=0,w=1;register char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*w;
}
int T,n,m,lastans,cnt,tot,q,k,s;
int last[N],dis[N],fa[N],f[N][25],H[N];
bool vis[N];
struct node{int id,d;};
struct Edge{int u,v,h;}E[N];
struct edge{int to,next,w;}e[N<<1];
bool operator <(node a,node b){return a.d>b.d;}
inline void link(ri u,ri v,ri w){
    e[++cnt]=(edge){v,last[u],w};last[u]=cnt;
    e[++cnt]=(edge){u,last[v],w};last[v]=cnt;
}
inline bool cmp(Edge a,Edge b){return a.h>b.h;}
priority_queue<node>Q;
inline void Dij(){
    memset(dis,63,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[1]=0;Q.push((node){1,0});
    while(!Q.empty()){
        ri u=Q.top().id;Q.pop();
        if(vis[u])continue;vis[u]=1;
        for(ri i=last[u],v;i;i=e[i].next){
            v=e[i].to;
            if(dis[v]>dis[u]+e[i].w){
                dis[v]=dis[u]+e[i].w;
                Q.push((node){v,dis[v]});
            }
        }
    }
}
int find(ri x){return x==fa[x]?x:fa[x]=find(fa[x]);}
inline void Kru(){
    tot=n;
    memset(f,0,sizeof(f));
    memset(H,0,sizeof(H));
    for(ri i=1;i<(n<<1);i++)fa[i]=i;
    sort(E+1,E+m+1,cmp);
    for(ri i=1,x,y;i<=m;i++){
        x=find(E[i].u);
        y=find(E[i].v);
        if(x==y)continue;
        fa[x]=fa[y]=f[x][0]=f[y][0]=++tot;
        H[tot]=E[i].h;
        dis[tot]=min(dis[x],dis[y]);
    }
    for(ri j=1;j<=20;j++)
        for(ri i=1;i<=tot;i++)
            f[i][j]=f[f[i][j-1]][j-1];
}
inline int Query(ri x,ri h){
    for(ri i=20;i>=0;i--)
        if(f[x][i]&&H[f[x][i]]>h)
            x=f[x][i];
    return dis[x];
}
int main(){
    T=re();
    while(T--){
        lastans=0;cnt=0;
        n=re(),m=re();
        memset(last,0,sizeof(last));
        for(ri i=1,u,v,l,a;i<=m;i++){
            u=re(),v=re(),l=re(),a=re();
            E[i]=(Edge){u,v,a};
            link(u,v,l);
        }
        Dij();
        Kru();
        q=re(),k=re(),s=re();
        ri v,p;
        while(q--){
            v=(re()+k*lastans-1)%n+1;
            p=(re()+k*lastans)%(s+1);
            printf("%d\n",lastans=Query(v,p));
        }
    }
    return 0;
}
				
				
				| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 | 
| Testcase #1 | 6.679 ms | 43 MB + 140 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #2 | 6.702 ms | 43 MB + 148 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #3 | 6.835 ms | 43 MB + 156 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #4 | 6.928 ms | 43 MB + 160 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #5 | 9.877 ms | 43 MB + 328 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #6 | 599.096 ms | 60 MB + 76 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #7 | 9.32 ms | 43 MB + 248 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #8 | 9.321 ms | 43 MB + 252 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #9 | 9.328 ms | 43 MB + 248 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #10 | 532.809 ms | 54 MB + 68 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #11 | 532.977 ms | 54 MB + 72 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #12 | 751.063 ms | 61 MB + 692 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #13 | 751.604 ms | 61 MB + 528 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #14 | 751.06 ms | 60 MB + 960 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #15 | 10.517 ms | 43 MB + 332 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #16 | 10.482 ms | 43 MB + 336 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #17 | 751.331 ms | 61 MB + 204 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #18 | 750.984 ms | 61 MB + 492 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #19 | 1.269 s | 64 MB + 624 KB | Accepted | Score: 5 | 显示更多 | 
| Testcase #20 | 1.275 s | 64 MB + 124 KB | Accepted | Score: 5 | 显示更多 |