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

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

b = A("b")
d = B("d")
k = B("k")
l = A("l")
i = A("i")
h = B("h")

b.z = l
b.x = l
d.r = l
k.r = b
l.z = b
l.x = i
i.z = b
i.x = b
h.r = l
assert d.r.z.x.z.txt == "
class A:
    def __init__(self, txt: str):
        self.z: A = None
        self.x: A = None
        self.txt = txt

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

b = A("b")
d = B("d")
k = B("k")
l = A("l")
i = A("i")
h = B("h")

b.z = l
b.x = l
d.r = l
k.r = b
l.z = b
l.x = i
i.z = b
i.x = b
h.r = l
assert d.r.z.x.z.txt == "b"