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

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

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

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

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

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

h.r = i
h.z = f
i.p = f
g.r = f
g.z = i
f.p = i
assert f.p.p.p.txt == "i"