#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#define int long long
#define pii pair<int,int>
#define mp(x,y) make_pair(x,y)
using namespace std;
struct Graph
{
struct sss
{
int q,w,e,r,nxt;
friend bool operator<(const sss&a,const sss&s){return a.r>s.r;}
}a[1000005];
int cnt,head[1000005];
void adde(int q,int w,int e,int r)
{
a[++cnt].q=q;
a[cnt].w=w;
a[cnt].e=e;
a[cnt].r=r;
a[cnt].nxt=head[q];
head[q]=cnt;
}
void clear(){memset(head,0,sizeof(head));cnt=0;}
}G1;
int dis[1000005],vis[1000005],cnt,n,fa[1000005],K,ans;
priority_queue<pii,vector<pii>,greater<pii>>q;
void dijstra()
{
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[1]=0;
q.push(mp(0,1));
while(!q.empty())
{
int x=q.top().second;
q.pop();
if(vis[x])continue;
vis[x]=1;
for(int i=G1.head[x];i;i=G1.a[i].nxt)
{
if(dis[G1.a[i].w]>dis[x]+G1.a[i].e)
{
dis[G1.a[i].w]=dis[x]+G1.a[i].e;
q.push(mp(dis[G1.a[i].w],G1.a[i].w));
}
}
}
}
int fnd(int x){return x==fa[x]?x:fa[x]=fnd(fa[x]);}
struct kruskal:public Graph
{
int root,f[1000005],num=n,g[1000005],p[1000005][25];
void dfs(int x,int fa)
{
g[x]=dis[x];
p[x][0]=fa;
for(int i=1;i<25;i++)p[x][i]=p[p[x][i-1]][i-1];
for(int i=head[x];i;i=a[i].nxt)
{
if(a[i].w!=fa)
{
dfs(a[i].w,x);
g[x]=min(g[x],g[a[i].w]);
}
}
}
void init()
{
num=n;
dijstra();
sort(G1.a+1,G1.a+G1.cnt+1);
for(int i=1;i<=2*n;i++)fa[i]=i;
for(int i=1;i<=n;i++)f[i]=1e18;
for(int i=1;i<=G1.cnt;i++)
{
int fx=fnd(G1.a[i].q);
int fy=fnd(G1.a[i].w);
if(fx!=fy)
{
++num;
adde(fx,num,0,0);
adde(num,fx,0,0);
adde(fy,num,0,0);
adde(num,fy,0,0);
fa[fx]=fa[fy]=num;
f[num]=G1.a[i].r;
}
}
root=num;
dfs(root,0);
}
int ask(int x,int v)
{
for(int i=24;~i;i--)if(f[p[x][i]]>v)x=p[x][i];
return g[x];
}
}T1;
int T,m,Q,S;
signed main()
{
scanf("%lld",&T);
while(T--)
{
ans=0;
G1.clear();
T1.clear();
scanf("%lld%lld",&n,&m);
for(int i=1;i<=m;i++)
{
int q,w,e,r;
scanf("%lld%lld%lld%lld",&q,&w,&e,&r);
G1.adde(q,w,e,r);
G1.adde(w,q,e,r);
}
T1.init();
scanf("%lld%lld%lld",&Q,&K,&S);
while(Q--)
{
int x,y;
scanf("%lld%lld",&x,&y);
x=(x+K*ans-1)%n+1;
y=(y+K*ans)%(S+1);
printf("%lld\n",ans=T1.ask(x,y));
}
}
}
/*
1
5 4
1 2 3 3
4 5 5 4
1 3 4 3
2 4 2 4
1 0 5
2 3
*/
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 5.305 ms | 30 MB + 592 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 5.337 ms | 30 MB + 596 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 5.53 ms | 30 MB + 636 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 5.74 ms | 30 MB + 680 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 11.24 ms | 31 MB + 820 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 1.021 s | 179 MB + 972 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 9.818 ms | 31 MB + 640 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 9.831 ms | 31 MB + 644 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 9.824 ms | 31 MB + 644 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 759.69 ms | 166 MB + 188 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 760.149 ms | 166 MB + 192 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 1.139 s | 184 MB + 432 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 1.136 s | 184 MB + 416 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 1.138 s | 184 MB + 440 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 11.805 ms | 31 MB + 844 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 11.831 ms | 31 MB + 848 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 1.138 s | 184 MB + 420 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 1.14 s | 184 MB + 428 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 1.839 s | 187 MB + 884 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 1.84 s | 187 MB + 924 KB | Accepted | Score: 5 | 显示更多 |