import java.util.*;

public class ShannonCircuits {

    static Map<Character, int[]> inputs = new HashMap<>();
    static Map<Character, String[]> gates = new HashMap<>();
    static int cycles;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Number of gates
        int numGates = sc.nextInt();
        sc.nextLine();

        // Read gates
        for (int i = 0; i < numGates; i++) {
            String gate = sc.nextLine();
            String[] parts = gate.split("=");
            char output = parts[0].trim().charAt(0);
            gates.put(output, parts[1].trim().split("[(), ]+"));
        }

        // Number of cycles
        cycles = sc.nextInt();
        sc.nextLine();

        // Read inputs
        for (int i = 0; i < numGates + 1; i++) {
            String[] inputLine = sc.nextLine().split(" ");
            char inputName = inputLine[0].charAt(0);
            int[] inputValues = new int[cycles];
            for (int j = 1; j <= cycles; j++) {
                inputValues[j - 1] = Integer.parseInt(inputLine[j]);
            }
            inputs.put(inputName, inputValues);
        }

        // Gate whose output is required
        char targetGate = sc.nextLine().charAt(0);

        // Compute output
        int[] output = computeOutput(targetGate, new HashSet<>());

        // Print output
        for (int i = 0; i < cycles; i++) {
            System.out.print(output[i] + " ");
        }
    }

    static int[] computeOutput(char gate, Set<Character> visited) {
        // To prevent infinite recursion in case of cyclic dependency
        if (visited.contains(gate)) {
            throw new IllegalArgumentException("Cyclic dependency detected in the gates.");
        }
        visited.add(gate);

        int[] output = new int[cycles];
        Arrays.fill(output, 0); // Initial output is 0

        if (!gates.containsKey(gate)) {
            return inputs.get(gate); // If gate is an input variable
        }

        String[] gateInfo = gates.get(gate);
        char input1 = gateInfo[1].charAt(0);
        char input2 = gateInfo[2].charAt(0);
        String gateType = gateInfo[0];

        int[] inputValues1 = inputs.containsKey(input1) ? inputs.get(input1) : computeOutput(input1, visited);
        int[] inputValues2 = inputs.containsKey(input2) ? inputs.get(input2) : computeOutput(input2, visited);

        for (int i = 1; i < cycles; i++) {
            switch (gateType) {
                case "AND":
                    output[i] = inputValues1[i - 1] & inputValues2[i - 1];
                    break;
                case "OR":
                    output[i] = inputValues1[i - 1] | inputValues2[i - 1];
                    break;
                case "NAND":
                    output[i] = ~(inputValues1[i - 1] & inputValues2[i - 1]) & 1;
                    break;
                case "NOR":
                    output[i] = ~(inputValues1[i - 1] | inputValues2[i - 1]) & 1;
                    break;
                case "XOR":
                    output[i] = inputValues1[i - 1] ^ inputValues2[i - 1];
                    break;
            }
        }

        return output;
    }
}
fix the Presentation Error