#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct node
{
char name[30];
int num;
} team[105];
int main()
{
int t,n,s,x,y,mod,i,j,cnt;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d%d",&n,&s,&x,&y,&mod);
for(i = 1; i<=n; i++)
scanf("%s request %d pages",team[i].name,&team[i].num);
cnt = s;
for(i = 1; i<=n; i++)
{
while(1)
{
if(team[i].num<=cnt)
{
printf("%d pages for %s\n",team[i].num,team[i].name);
cnt-=team[i].num;
break;
}
else
{
printf("%d pages for %s\n",cnt,team[i].name);
s = (s*x+y)%mod;
cnt = s;
}
}
}
printf("\n");
}
return 0;
}