#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
#define FIN freopen("input.txt","r",stdin)
#define mem(x,y) memset(x,y,sizeof(x))
typedef unsigned long long ULL;
typedef long long LL;
#define fuck(x) cout<<"["<<x<<"]"<<endl;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef pair<pair<int,int>,int> PIII;
typedef pair<int,int> PII;
const double eps=1e-6;
const int MX=1e2+5;
LL P;
LL e,p,q,n;
LL exgcd(LL a,LL b,LL &x,LL &y)
{
if(b==0)
{
x=1,y=0;
return a;
}
LL d=exgcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
//ax+by=c
LL solve(LL a,LL b,LL c,LL &x,LL &y)
{
LL d;
if(c%(d=__gcd(a,b)))return -1;//返回-1是无解
a/=d;
b/=d;
c/=d;
exgcd(a,b,x,y);
x=((LL)x*c)%b;
while(x<=0)x+=b;
}
LL w[MX];
LL multi(LL a,LL b)
{
LL ans=0;
while(b)
{
if(b&1)ans=(ans+a)%P;
a=(a+a)%P;
b>>=1;
}
return ans;
}
LL quick_pow(LL a,LL x)
{
LL ans=1;
while(x)
{
if(x&1)ans=multi(ans,a);
a=multi(a,a);
x>>=1;
}
return ans;
}
int main()
{
FIN;
int t;
cin>>t;
while(t--)
{
cin>>e>>p>>q>>n;
P=p*q;
LL x,y;
solve(e,(p-1)*(q-1),1,x,y);
for(int i=1; i<=n; i++)cin>>w[i];
for(int i=1; i<=n; i++)
{
w[i]=quick_pow(w[i],x);
printf("%lld%c",w[i],((i==n)?'\n':' '));
}
}
return 0;
}