import std.stdio;
import std.ascii;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;
 
void main() {
    string s;
    int[] bits;

    bool input() {
        s = readln.chomp;
        if (s == "END") return false;
        bits.clear;
        int N; scanf("%d\n", &N);
        foreach (i; 0 .. N) {
            int bit = 0;
            foreach (j; 0 .. 4) {
                int x; scanf("%d", &x);
                bit |= (x << j);
            }
            scanf("\n");
            bits ~= bit;
        }
        return true;
    }

    class Node {
        Node left, right;
        int bit;
        this(Node left, Node right) {
            this.left = left;
            this.right = right;
        }
        this(int bit) {
            this.bit = bit;
            this.left = null;
            this.right = null;
        }
        bool leaf() {
            return left is null && right is null;
        }
    }
    Node parse() {
        int index = 0;
        //writeln(s);
        Node dfs() {
            if (s[index] == '(') {
                index++;
                Node left = dfs;
                assert(s[index] == ' ');
                index++;
                Node right = dfs;
                assert(s[index] == ')');
                index++;
                return new Node(left, right);
            } else if (isDigit(s[index])) {
                int bit_i = s[index] - '1';
                index++;
                return new Node(bits[bit_i]);
            } else {
                assert(0);
            }
        }
        return dfs;
    }

    void solve() {
        Node root = parse;
        const zs = "AOX";
        int[] dfs(Node node) {
            if (node.leaf) return [node.bit];
            int[] ret;
            int[] left = dfs(node.left),
                  right = dfs(node.right);
            foreach (a; left) {
                foreach (b; right) {
                    foreach (z; zs) {
                        if (z == 'A') {
                            ret ~= a & b;
                        } else if (z == 'O') {
                            ret ~= a | b;
                        } else if (z == 'X') {
                            ret ~= a ^ b;
                        } else {
                            assert(0);
                        }
                    }
                }
            }
            return ret;
        }
        writeln(dfs(root).count(15));
    }

    while (input) solve;
}