https://huggingface.co/spaces/yuntian-deng/o1mini
""
import sys

class Node:
    def __init__(self, val):
        self.val = val
        self.uid = None
        self.parent = None  
        self.child = [] 
        self.isLocked = False  
        self.lockedDescendantCount = 0  

class Tree:
    def __init__(self, m, n, name_list):
        self.node_list = []
        self.node_map = {}

        for name in name_list:
            node = Node(name)
            self.node_list.append(node)
            self.node_map[name] = node

        # Build the tree
        for i in range(n):
            for j in range(m):
                child_index = m * i + j + 1
                if child_index < n:
                    self.node_list[i].child.append(self.node_list[child_index])
                    self.node_list[child_index].parent = self.node_list[i]

        self.root = self.node_list[0]

    def lock(self, node, uid):
        if node.isLocked:
            return False

        if self.isAncestorLocked(node):
            return False

        if node.lockedDescendantCount > 0:
            return False

        # Lock the node
        node.isLocked = True
        node.uid = uid

        # Update the lockedDescendantCount for ancestors
        parent = node.parent
        while parent:
            parent.lockedDescendantCount += 1
            parent = parent.parent

        return True

    def unlock(self, node, uid):
        if not node.isLocked or node.uid != uid:
            return False

        # Unlock the node
        node.isLocked = False
        node.uid = None

        # Update the lockedDescendantCount for ancestors
        parent = node.parent
        while parent:
            parent.lockedDescendantCount -= 1
            parent = parent.parent

        return True

    def upgrade(self, node, uid):
        if node.isLocked:
            return False

        if self.isAncestorLocked(node):
            return False

        if node.lockedDescendantCount == 0:
            return False

        locked_descendants = []

        # Collect all locked descendants
        if not self.collectLockedDescendants(node, uid, locked_descendants):
            return False

        # Unlock all locked descendants
        for descendant in locked_descendants:
            descendant.isLocked = False
            descendant.uid = None

            # Update the lockedDescendantCount for ancestors
            parent = descendant.parent
            while parent:
                parent.lockedDescendantCount -= 1
                parent = parent.parent

        # Lock the current node
        node.isLocked = True
        node.uid = uid

        # Update the lockedDescendantCount for ancestors
        parent = node.parent
        while parent:
            parent.lockedDescendantCount += 1
            parent = parent.parent

        return True

    def isAncestorLocked(self, node):
        curr = node.parent
        while curr:
            if curr.isLocked:
                return True
            curr = curr.parent
        return False

    def collectLockedDescendants(self, node, uid, locked_nodes):
        if node.isLocked:
            if node.uid == uid:
                locked_nodes.append(node)
            else:
                return False  # Locked by different uid

        for child_node in node.child:
            if not self.collectLockedDescendants(child_node, uid, locked_nodes):
                return False
        return True

    def process_queries(self, queries):
        results = []
        for query in queries:
            operation, node_name, uid = query.strip().split()
            uid = int(uid)
            node = self.node_map[node_name]
            if operation == "1":
                result = self.lock(node, uid)
            elif operation == "2":
                result = self.unlock(node, uid)
            elif operation == "3":
                result = self.upgrade(node, uid)
            else:
                result = False  # Invalid operation
            results.append('true' if result else 'false')
        return results

def main():
    n = int(sys.stdin.readline())
    m = int(sys.stdin.readline())
    q = int(sys.stdin.readline())
    name_list = [sys.stdin.readline().strip() for _ in range(n)]
    queries = [sys.stdin.readline().strip() for _ in range(q)]
    tree = Tree(m, n, name_list)
    results = tree.process_queries(queries)
    for res in results:
        print(res)

main()

"""