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

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

h = A("h")
a = B("a")
g = B("g")
f = A("f")

h.p = f
a.u = f
a.z = f
g.u = h
g.z = f
f.p = h
assert a.z.p.p.txt == "
class A:
    def __init__(self, txt: str):
        self.p: A = None
        self.txt = txt

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

h = A("h")
a = B("a")
g = B("g")
f = A("f")

h.p = f
a.u = f
a.z = f
g.u = h
g.z = f
f.p = h
assert a.z.p.p.txt == "f"