# Arup Guha
# 11/24/2021
# Solution to 2021 CIS 3362 Fall 2021 Homework #7: Group Diffie-Hellman

# Constants for indexes into my node "struct"
KEY = 0
PARENT = 1
LCHILD = 2
RCHILD = 3

# Returns b to the e mod mod, using fast modular exponentiation.
def fastModExpo(b,e,mod):
    if e == 0:
        return 1%mod
    if e%2 == 0:
        tmp = fastModExpo(b,e//2,mod)
        return (tmp*tmp)%mod
    return (b*fastModExpo(b,e-1,mod))%mod

# Forms the key between secret keys key1 and key2 using public keys
# prime = mod and generator = base.
def formKey(key1, key2, mod, base):
    return fastModExpo(base, key1*key2, mod)

# This recalculates keys up the ancestral chain, starting from the node
# with the label bk.
def fix(keyMap, bk, mod, base):

    # Made it past the root.
    if bk == "":
        return

    # This is lengthy, so I store each in temp vars.
    lKey = keyMap[keyMap[bk][LCHILD]][KEY]
    rKey = keyMap[keyMap[bk][RCHILD]][KEY]

    # Fix this key.
    keyMap[bk][KEY] = formKey(lKey, rKey, mod, base)

    fix(keyMap, keyMap[bk][PARENT], mod, base)

# Adds the person p2 with key k2 to the tree stored in keymap with
# sponsor p1, who will make a new key k1. The new shared node will
# be called bk. mod and base are the prime and generator respectively,
# for the public keys.
def addPerson(keyMap, p1, k1, p2, k2, bk, mod, base):

    # Store the parent of the sponsor.
    myPar = keyMap[p1][PARENT]

    # See which kid of my parent I am.
    myDir = LCHILD
    if keyMap[myPar][RCHILD] == p1:
        myDir = RCHILD

    # New node for sponsor.
    keyMap[p1] = [k1, bk, "", ""]

    # New node for new person added.
    keyMap[p2] = [k2, bk, "", ""]

    # This is the blind key node, without the key fixed.
    keyMap[bk] = [-1, myPar, p1, p2]

    # Link parent of bk to bk.
    keyMap[myPar][myDir] = bk

    # This fixes the chain of keys from the node bk on.
    fix(keyMap, bk, mod, base)

# Delets the node storing p1 from the tree stored in keyMap.
def delPerson(keyMap, p1, k1, mod, base):

    # Parent and grandparent of deleted node.
    myPar = keyMap[p1][PARENT]
    myGP = keyMap[myPar][PARENT]

    # Get which child myPar is of myGP.
    mydir = LCHILD
    if keyMap[myGP][RCHILD] == myPar:
        mydir = RCHILD

    # Find sibling of deleted node - it's left or right of parent.
    mySib = keyMap[myPar][LCHILD]
    if mySib == p1:
        mySib = keyMap[myPar][RCHILD]

    # My leaf node check - update key stored here.
    if keyMap[mySib][LCHILD] == "":
        keyMap[mySib][KEY] = k1

    # The new parent of the sibling is the grandparent.
    keyMap[mySib][PARENT] = myGP

    # Link directly to sibling node, from grandparent.
    keyMap[myGP][mydir] = mySib

    # These two nodes disappear.
    del keyMap[myPar]
    del keyMap[p1]

    # First key that needs to be fixed is the grand parent key...
    fix(keyMap, myGP, mod, base)

# Runs the program.
def main():

    # Get both the prime and generator.
    toks = input().split()
    p = int(toks[0])
    g = int(toks[1])

    # Get the number of commands.
    numC = int(input(""))

    # Will store the tree via a map...key = name, value = [key,par,left,right].
    keyMap = {}

    # Deal with the first line separately.
    toks = input().split()
    name1 = toks[0]
    key1 = int(toks[1])
    name2 = toks[2]
    key2 = int(toks[3])
    nameP = toks[4]

    # Put in these three nodes into the tree.
    keyMap[name1] = [key1, nameP, "", ""]
    keyMap[name2] = [key2, nameP, "", ""]
    keyMap[nameP] = [formKey(key1, key2, p, g), "", name1, name2]

    # Process the rest of the commands.
    for loop in range(numC-1):

        
        toks = input().split()

        # If there are 2 items, it's a query.
        if len(toks) == 2:
            print(keyMap[toks[1]][KEY])

        # This is a delete.
        elif len(toks) == 3:
            delPerson(keyMap, toks[1], int(toks[2]), p, g)

        # Must be an add.
        else:
            addPerson(keyMap, toks[1], int(toks[2]), toks[3], int(toks[4]), toks[5], p, g)

# Start it!
main()
    
