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

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

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

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

a = A("a")
m = B("m")
b = C("b")
e = D("e")
f = D("f")
d = C("d")
i = B("i")

a.y = i
a.q = e
m.r = i
b.q = i
e.p = a
f.p = a
d.q = i
i.r = m
assert b.q.r.r.r.txt == "
class A:
    def __init__(self, txt: str):
        self.y: B = None
        self.q: D = None
        self.txt = txt

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

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

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

a = A("a")
m = B("m")
b = C("b")
e = D("e")
f = D("f")
d = C("d")
i = B("i")

a.y = i
a.q = e
m.r = i
b.q = i
e.p = a
f.p = a
d.q = i
i.r = m
assert b.q.r.r.r.txt == "m"