#include <bits/stdc++.h>
#define L(i, j, k) for (int i = (j); i <= (k); i++)
#define R(i, j, k) for (int i = (j); i >= (k); i--)
using namespace std;
using LL = long long;
const int N = 15, M = 1e6 + 10;
const LL V = 1e15;
LL f[N], g[N], mod;
int T;
map<LL, LL> dF, dG;
map<LL, int> cc;
template<class T = int> T read() {
T x = 0, y = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') {
y = -1;
}
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * y;
}
void push(LL cur, vector<LL> &v) {
if (cc[cur]) {
return;
}
cc[cur] = 1;
if (cur >= 14) {
v.emplace_back(cur);
}
for (int p : {2, 3, 5, 7}) {
if (cur <= V / p) {
push(cur * p, v);
}
}
}
LL getF(LL p) {
return dF.find(p) == dF.end() ? 0 : dF[p];
}
LL getG(LL p) {
return dG.find(p) == dG.end() ? 0 : dG[p];
}
int main() {
f[0] = read(), g[0] = read(), T = read();
mod = read<LL>();
L(i, 1, 13) {
f[i] = max((LL)i, g[i / 2] + g[i / 3] + g[i / 5] + g[i / 7]);
g[i] = max((LL)i, f[i / 2] + f[i / 3] + f[i / 4] + f[i / 5]);
dF[i] = (f[i] - f[i - 1]) % mod;
dG[i] = (g[i] - g[i - 1]) % mod;
}
vector<LL> vec;
push(1, vec);
push(11, vec);
push(13, vec);
sort(vec.begin(), vec.end());
for (int p : vec) {
dF[p] = (LL)(!(p % 2) * getG(p / 2) + !(p % 3) * getG(p / 3) + !(p % 5) * getG(p / 5) + !(p % 7) * getG(p / 7) % mod;
dG[p] = (LL)(!(p % 2) * getF(p / 2) + !(p % 3) * getF(p / 3) + !(p % 4) * getF(p / 4) + !(p % 5) * getF(p / 5)) % mod;
}
int cnt = 0;
vector<pair<LL, LL>> F, G;
F.push_back({0, f[0]});
G.push_back({0, g[0]});
for (auto t : dF) {
F.push_back(t);
}
for (auto t : dG) {
G.push_back(t);
}
assert(F.size() == G.size());
L(i, 1, (int)F.size() - 1) {
(F[i].second += F[i - 1].second) %= mod;
(G[i].second += G[i - 1].second) %= mod;
}
while (T--) {
LL m = read<LL>();
printf("%lld %lld\n", (*--upper_bound(F.begin(), F.end(), make_pair(m, V))).second, (*--upper_bound(G.begin(), G.end(), make_pair(m, V))).second);
}
return 0;
}