import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines B = 5000 N, S, X, Y, Z = map(int, readline().split()) def gen_A(): x = S for i in range(N): yield x & 1 x = X * x + Y x %= Z A_str = ''.join(map(str, gen_A())) nums = [] for i in range((N + B - 1) // B): nums.append(int(A_str[B * i:B * i + B][::-1], 2)) def get(L, R): i = L // B L -= B * i R -= B * i if B >= R: x = nums[i] >> L else: x = (nums[i] >> L) ^ (nums[i + 1] << B - L) return x & ((1 << R - L) - 1) def add_local(i, L, x): nums[i] ^= x << L def add(L, R, x): i = L // B L -= B * i R -= B * i if B >= R: add_local(i, L, x) return left = x & ((1 << B - L) - 1) right = x >> (B - L) add_local(i, L, left) add_local(i + 1, 0, right) Q = int(readline()) m = map(int, read().split()) for s, t, u, v in zip(m, m, m, m): x = get(s - 1, t) add(u - 1, v, x)