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

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

f = A("f")
h = B("h")
a = A("a")
k = B("k")
d = A("d")

f.p = h
h.r = d
h.z = k
a.p = h
k.r = f
k.z = h
d.p = h
assert d.p.r.p.z.txt == "
class A:
    def __init__(self, txt: str):
        self.p: B = None
        self.txt = txt

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

f = A("f")
h = B("h")
a = A("a")
k = B("k")
d = A("d")

f.p = h
h.r = d
h.z = k
a.p = h
k.r = f
k.z = h
d.p = h
assert d.p.r.p.z.txt == "k"