#include <iostream>
#include <cstring>
using namespace std;
const int MN = 10111000;
int N, cnt[MN];
long long S, C1, C2, M;
int main() {
ios :: sync_with_stdio(false); cin.tie(NULL);
int test; cin >> test;
while (test--) {
int K;
cin >> N >> K >> S >> C1 >> C2 >> M;
memset(cnt, 0, sizeof cnt);
long long A = 0;
for(int i = 1; i <= N; ++i) {
if (i == 1) {
A = S;
} else {
A = (C1 * A + C2) % M;
}
++cnt[A];
}
for(int i = 0; i < MN; ++i) {
if (K <= 0) break;
int cur = min(K, cnt[i]);
K -= cur;
while (cur--) {
cout << i << ' ';
}
}
cout << endl;
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0cmluZz4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNvbnN0IGludCBNTiA9IDEwMTExMDAwOwoKaW50IE4sIGNudFtNTl07CmxvbmcgbG9uZyBTLCBDMSwgQzIsIE07CgppbnQgbWFpbigpIHsKICAgIGlvcyA6OiBzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOyBjaW4udGllKE5VTEwpOwoKICAgIGludCB0ZXN0OyBjaW4gPj4gdGVzdDsKICAgIHdoaWxlICh0ZXN0LS0pIHsKICAgICAgICBpbnQgSzsKICAgICAgICBjaW4gPj4gTiA+PiBLID4+IFMgPj4gQzEgPj4gQzIgPj4gTTsKCiAgICAgICAgbWVtc2V0KGNudCwgMCwgc2l6ZW9mIGNudCk7CiAgICAgICAgbG9uZyBsb25nIEEgPSAwOwogICAgICAgIGZvcihpbnQgaSA9IDE7IGkgPD0gTjsgKytpKSB7CiAgICAgICAgICAgIGlmIChpID09IDEpIHsKICAgICAgICAgICAgICAgIEEgPSBTOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgQSA9IChDMSAqIEEgKyBDMikgJSBNOwogICAgICAgICAgICB9CiAgICAgICAgICAgICsrY250W0FdOwogICAgICAgIH0KCiAgICAgICAgZm9yKGludCBpID0gMDsgaSA8IE1OOyArK2kpIHsKICAgICAgICAgICAgaWYgKEsgPD0gMCkgYnJlYWs7CgogICAgICAgICAgICBpbnQgY3VyID0gbWluKEssIGNudFtpXSk7CiAgICAgICAgICAgIEsgLT0gY3VyOwogICAgICAgICAgICB3aGlsZSAoY3VyLS0pIHsKICAgICAgICAgICAgICAgIGNvdXQgPDwgaSA8PCAnICc7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgY291dCA8PCBlbmRsOwogICAgfQp9Cg==