#include<cstdio>
#include<cstring>
#include<algorithm>
#define lowbit(x) (x&(-x))
using namespace std;
typedef unsigned int uint;
const int N=1000005,lim=1500000;
uint seed,last=7;
int n,m,Ac[N],Time[N];
uint Rand(uint &seed,uint last){
seed=seed17+last;
return seed%n+1;
}
namespace BIT{
int bit[N];
void Clear(){
memset(bit,0,sizeof(bit));
}
void add(int x,int val){
for(;x<N;x+=lowbit(x)) bit[x]+=val;
}
int ask(int x,int ans=0){
for(;x;x-=lowbit(x)) ans+=bit[x];
return ans;
}
};
using namespace BIT;
namespace SEG{
struct node{
int lc,rc,num;
}Seg[N40];
int tot,root[N];
void init(){
tot=0;
memset(root,0,sizeof(root));
}
#define mid ((l+r)>>1)
void Modify(int &root,int l,int r,int pos,int v){
if(!root){
root=++tot;
Seg[root].lc=Seg[root].rc=Seg[root].num=0;
}
if(l==r) {Seg[root].num+=v;return;}
if(pos<=mid) Modify(Seg[root].lc,l,mid,pos,v);
else Modify(Seg[root].rc,mid+1,r,pos,v);
Seg[root].num=Seg[Seg[root].lc].num+Seg[Seg[root].rc].num;
}
int Query(int &root,int l,int r,int x,int y){
if(!root) return 0;
if(l>=x&&r<=y) return Seg[root].num;
if(y<=mid) return Query(Seg[root].lc,l,mid,x,y);
if(x> mid) return Query(Seg[root].rc,mid+1,r,x,y);
return Query(Seg[root].lc,l,mid,x,y)+Query(Seg[root].rc,mid+1,r,x,y);
}
}
using namespace SEG;
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d%u",&n,&m,&seed),Clear(),init();
memset(Ac,0,sizeof(Ac)),memset(Time,0,sizeof(Time));
for(int i=1;i<=n;i) Ac[i]=Time[i]=1;
add(1,n),Modify(root[1],1,lim,1,n);
while(m--){
int x=Rand(seed,last),y=Rand(seed,last);
add(Ac[x],-1),Modify(root[Ac[x]],1,lim,Time[x],-1);
Ac[x],Time[x]+=y;
add(Ac[x],1),Modify(root[Ac[x]],1,lim,Time[x],1);
last=n-ask(Ac[x]),last+=Query(root[Ac[x]],1,lim,1,Time[x]-1);
printf("%d\n",last);
}
}
return 0;
}