import sys input = sys.stdin.readline class Dinic: class Edge: def __init__(self, v, cap, rev): self.v = v self.cap = cap self.rev = rev def __init__(self, V): self.V = V self.G = [[] for _ in range(V)] def add_edge(self, u, v, cap): self.G[u].append(self.Edge(v, cap, len(self.G[v]))) self.G[v].append(self.Edge(u, 0, len(self.G[u])-1)) # calculate the shortest distance from s def bfs(self, s): from collections import deque self.level = [-1] * self.V self.level[s] = 0 queue = deque() queue.append(s) while queue: u = queue.popleft() for e in self.G[u]: if e.cap > 0 and self.level[e.v] == -1: self.level[e.v] = self.level[u] + 1 queue.append(e.v) # find a path def dfs(self, u, t, f): if u == t: return f i = self.iter[u] while i < len(self.G[u]): e = self.G[u][i] if e.cap > 0 and self.level[u] < self.level[e.v]: d = self.dfs(e.v, t, min(f, e.cap)) if d > 0: e.cap -= d self.G[e.v][e.rev].cap += d return d i += 1 self.iter[u] += 1 return 0 # find the max flow from s to v def max_flow(self, s, t): flow = 0 INF = 10**18 while True: self.bfs(s) if self.level[t] == -1: return flow self.iter = [0] * self.V while True: f = self.dfs(s, t, INF) if f == 0: break flow += f def min_cut(self, s): visited = [False] * self.V st = [s] visited[s] = True while st: u = st.pop() for e in self.G[u]: if e.cap > 0 and not visited[e.v]: visited[e.v] = True st.append(e.v) return visited N, M, K, P = map(int, input().split()) E = list(map(int, input().split())) F = list(map(int, input().split())) V = list(map(int, input().split())) flow = Dinic(N+M+K+2) s = N+M+K t = s+1 ans = 0 for i in range(N): flow.add_edge(s, i, E[i]) flow.add_edge(i, t, 0) ans += E[i] for i in range(M): flow.add_edge(s, N+i, 0) flow.add_edge(N+i, t, F[i]) ans += F[i] for i in range(K): flow.add_edge(s, N+M+i, 0) flow.add_edge(N+M+i, t, V[i]) for i in range(N): _, *A = list(map(int, input().split())) for a in A: a -= 1 flow.add_edge(i, N+M+a, 10**18) for i in range(P): I, J = map(int, input().split()) I -= 1 J -= 1 flow.add_edge(I, N+J, 10**18) flow.add_edge(N+J, I, 10**18) ans -= flow.max_flow(s, t) use = flow.min_cut(s) goals = [] actions = [] preps = [] for i in range(N+M+K): if i < N: if use[i]: goals.append(i+1) elif i < N+M: if not use[i]: actions.append(i-N+1) else: if use[i]: preps.append(i-N-M+1) print(ans) print(len(goals) + len(actions) + len(preps)) for i in preps: print("Preparation", i) for i in goals: print("Goal", i) for i in actions: print("Action", i)