#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
#define CRP(t, x) const t &x
#define OPL(t, x) bool operator<(CRP(t, x)) const
#define FIL(x, v) memset(x, v, sizeof(x))
#define CLR(x) FIL(x, 0)
#define NE1(x) FIL(x, -1)
#define INF(x) FIL(x, 0x3f)
#ifndef _DEBUG
#define _DEBUG 0
#endif // !_DEBUG
#define IFD if (_DEBUG)
typedef long long ll, i64;
const int N = 4e5 + 50;
int X[N], Y[N], mp[N << 1];
template <size_t sz>
struct SegTree
{
#define L(x) ((x) << 1)
#define R(x) ((x) << 1 | 1)
#define avg(x, y) (((x) + (y)) >> 1)
    struct Node
    {
        ll a, x;
    } N[sz << 2];
    void addtag(int k, int l, int r, ll v)
    {
        N[k].a += v;
        N[k].x += (r - l) * v;
    }
    void pushdown(int k, int l, int r)
    {
        if (N[k].a == 0) return;
        int m = avg(l, r);
        addtag(L(k), l, m, N[k].a);
        addtag(R(k), m, r, N[k].a);
        N[k].a = 0;
    }
    void modify(int k, int l, int r, int x, int y, ll v)
    {
        if (x <= l && r <= y) return addtag(k, l, r, v);
        int m = avg(l, r);
        pushdown(k, l, r);
        if (x < m) modify(L(k), l, m, x, y, v);
        if (y > m) modify(R(k), m, r, x, y, v);
        N[k].x = N[L(k)].x + N[R(k)].x;
    }
    int kth(int k, int l, int r, int sz)
    {
        if (l == r - 1)
        {
            return l;
        }
        pushdown(k, l, r);
        int d = N[L(k)].x, m = avg(l, r);
        return d >= k ? kth(L(k), l, m, k) : kth(R(k), m, r, k - d);
    }
};
SegTree<N> st;
int main()
{
    int n;
    scanf("%d", &n);
    {
        ll a, b, c, m;
        scanf("%d%d%lld%lld%lld%lld", X, X + 1, &a, &b, &c, &m);
        for (int i = 2; i < n; i++) X[i] = (a * X[i - 1] + b * X[i - 2] + c) % m;
        scanf("%d%d%lld%lld%lld%lld", Y, Y + 1, &a, &b, &c, &m);
        for (int i = 2; i < n; i++) Y[i] = (a * Y[i - 1] + b * Y[i - 2] + c) % m;
        for (int i = 0; i < n; i++)
        {
            if (X[i] > Y[i]) swap(X[i], Y[i]);
            X[i]++, Y[i]++;
        }
    }
    memcpy(mp, X, n << 2);
    memcpy(mp + n, Y, n << 2);
    sort(mp, mp + n + n);
    int mpl = unique(mp, mp + n + n) - mp;
    for (int i = 0; i < n; i++) X[i] = lower_bound(mp, mp + mpl, X[i]) - mp;
    for (int i = 0; i < n; i++) Y[i] = lower_bound(mp, mp + mpl, Y[i]) - mp;
    IFD
    {
        for (int i = 0; i < n; i++) printf("%d%c", mp[X[i]], " \n"[i == n - 1]);
        for (int i = 0; i < n; i++) printf("%d%c", mp[Y[i]], " \n"[i == n - 1]);
    }
    for (int i = 0; i < n; i++)
    {
    }
    return 0;
}