class A:
    def __init__(self, txt: str):
        self.z: B = None
        self.txt = txt

class B:
    def __init__(self, txt: str):
        self.p: B = None
        self.y: A = None
        self.txt = txt

n = A("n")
k = B("k")
h = B("h")
g = A("g")

n.z = h
k.p = h
k.y = g
h.p = k
h.y = n
g.z = k
assert g.z.p.p.txt == "
class A:
    def __init__(self, txt: str):
        self.z: B = None
        self.txt = txt

class B:
    def __init__(self, txt: str):
        self.p: B = None
        self.y: A = None
        self.txt = txt

n = A("n")
k = B("k")
h = B("h")
g = A("g")

n.z = h
k.p = h
k.y = g
h.p = k
h.y = n
g.z = k
assert g.z.p.p.txt == "k"