1 #include<cstdio>
2 #include<cstring>
3 #include<iostream>
4 #include<cmath>
5 #define ll long long
6 #define mem(a,b) memset(a,b,sizeof(a))
7 using namespace std;//5137
8 int maxn(int a,int b){return a>b?a:b;}
9 int abss(int x){return x<0?-x:x;}
10 const int Max=50050;
11
12 int prime[100001],num;
13 bool he[100001];
14
15 __attribute__((optimize("O3"))) void PRIME()
16 {
17 int q1=(int)ceil(sqrt(Max));
18 for(int i=2;i<=Max;++i)
19 {
20 if(he[i])continue;
21 prime[++num]=i;
22 for(int j=i*2;j<=Max;j+=i)
23 he[j]=1;
24 }
25 }
26
27
28
29 int p,q,k,m;
30 int h;
31 ll a,b;
32
33 __attribute__((optimize("O3"))) ll mi(ll x,ll c)
34 {
35 ll ans=1;
36 while(c)
37 {
38 if(c&1)
39 ans=ans*x%k;
40 x=x*x%k;
41 c>>=1;
42 }
43 return ans;
44 }
45
46 int azhi[5201],bzhi[5201];
47 /*void out11()
48 {
49 printf("\n");
50 for(int i=1;i<=20;++i)
51 printf("%d ",azhi[i]);
52 printf("\n");
53 for(int i=1;i<=20;++i)
54 printf("%d ",bzhi[i]);
55 printf("\n");
56 }*/
57 __attribute__((optimize("O3"))) void qq()
58 {
59 if(h&1||h==0)
60 {
61 a=0;
62 b=0;
63 return ;
64 }
65 mem(azhi,0);
66 mem(bzhi,0);
67 int ci=h/2-1;
68 int q1=2*q*q-2*p*q+p*p;
69 int q2=2*q*(p-q);
70 int q3=p;
71 //printf("ci=%lld q1=%lld q2=%lld q3=%lld\n",ci,q1,q2,q3);
72 int maxl=0;
73 //cout<<1;
74 int now=0;
75 while(q2!=1)
76 {
77 ++now;
78 while(!(q2%prime[now]))
79 {
80 q2/=prime[now];
81 ++azhi[now];
82 }
83 azhi[now]*=ci;
84 }
85 maxl=maxn(maxl,now);
86
87 now=0;
88 while(q1!=1)
89 {
90 ++now;
91 while(!(q1%prime[now]))
92 {
93 q1/=prime[now];
94 ++azhi[now];
95 }
96 }
97 maxl=maxn(maxl,now);
98
99 now=0;
100 while(q3!=1)
101 {
102 ++now;
103 while(!(q3%prime[now]))
104 {
105 q3/=prime[now];
106 ++bzhi[now];
107 }
108 bzhi[now]*=h;
109 }
110 maxl=maxn(maxl,now);
111
112 for(int i=1;i<=maxl;++i)
113 {
114 if(azhi[i]>bzhi[i]){azhi[i]-=bzhi[i];bzhi[i]=0;}
115 else{bzhi[i]-=azhi[i];azhi[i]=0;}
116 }
117 //out11();
118 a=1;b=1;
119 for(int i=1;i<=maxl;++i)
120 {
121 if(azhi[i])
122 a=(a*(mi(prime[i],azhi[i])%k))%k;
123 if(bzhi[i])
124 b=(b*(mi(prime[i],bzhi[i])%k))%k;
125 }
126 }
127
128 __attribute__((optimize("O3"))) int main(){
129 //freopen("1.txt","r",stdin);
130 //freopen("brtree7.in","r",stdin);
131 //freopen("brtree.out","w",stdout);
132 PRIME();
133 //printf("num=%d\n",num);
134 //out11();
135 scanf("%d%d%d%d",&q,&p,&m,&k);
136
137 while(m--)
138 {
139 scanf("%d",&h);
140 h-=a%k;
141 qq();
142 //printf("caocoaca\n");
143 printf("%lld %lld\n",a,b);
144 }
145 //printf("\nend\n");
146 //while(1);
147 return 0;
148 }