import bisect def coordinate_compression(a): n = len(a) x = sorted(list(set(a))) b = [0] * n for i in range(n): b[i] = bisect.bisect_left(x, a[i]) return b def bit_matrix_rank(a): n, m = len(a), len(a[0]) i, j = 0, 0 while i < n and j < m: i2 = i while i2 < n and not a[i2][j]: i2 += 1 if i2 == n: j += 1 continue a[i], a[i2] = a[i2], a[i] for i2 in range(n): if i2 == i: continue if a[i2][j]: for j2 in range(m): a[i2][j2] ^= a[i][j2] i += 1 j += 1 return i N, M, B, Q = map(int, input().split()) ijs = [] for _ in range(M): i, j = map(int, input().split()) ijs += [i, j] ijs_cp = coordinate_compression(ijs) h = max(ijs_cp) + 1 mat = [[0 for j in range(M)] for i in range(h)] for m in range(M): for t in range(2): mat[ijs_cp[2 * m + t]][m] = 1 mat2 = [[0 for j in range(M)] for i in range(Q)] for q in range(Q): m = list(map(int, input().split())) for t in range(3): mat2[q][m[t]] = 1 r = bit_matrix_rank(mat) r2 = bit_matrix_rank(mat2) resT = pow(2, r2, B) resS = (pow(2, M - r, B) - resT + B) % B print(resS, resT)