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

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

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

class D:
    def __init__(self, txt: str):
        self.o: C = None
        self.w: A = None
        self.txt = txt

d = A("d")
m = B("m")
a = C("a")
j = D("j")
g = C("g")

d.q = m
m.q = j
m.r = j
m.v = d
m.z = d
a.t = d
j.o = g
j.w = d
g.t = d
assert g.t.q.r.o.t.txt == "
class A:
    def __init__(self, txt: str):
        self.q: B = None
        self.txt = txt

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

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

class D:
    def __init__(self, txt: str):
        self.o: C = None
        self.w: A = None
        self.txt = txt

d = A("d")
m = B("m")
a = C("a")
j = D("j")
g = C("g")

d.q = m
m.q = j
m.r = j
m.v = d
m.z = d
a.t = d
j.o = g
j.w = d
g.t = d
assert g.t.q.r.o.t.txt == "d"