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

class B:
    def __init__(self, txt: str):
        self.x: C = None
        self.q: A = None
        self.txt = txt

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

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

l = A("l")
d = B("d")
c = C("c")
b = D("b")
h = C("h")
m = A("m")

l.o = h
l.y = m
l.r = d
d.x = c
d.q = l
c.r = l
b.y = d
h.r = m
m.o = c
m.y = l
m.r = d
assert l.r.x.r.txt == "
class A:
    def __init__(self, txt: str):
        self.o: C = None
        self.y: A = None
        self.r: B = None
        self.txt = txt

class B:
    def __init__(self, txt: str):
        self.x: C = None
        self.q: A = None
        self.txt = txt

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

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

l = A("l")
d = B("d")
c = C("c")
b = D("b")
h = C("h")
m = A("m")

l.o = h
l.y = m
l.r = d
d.x = c
d.q = l
c.r = l
b.y = d
h.r = m
m.o = c
m.y = l
m.r = d
assert l.r.x.r.txt == "l"