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

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

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

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

a = A("a")
b = B("b")
g = C("g")
k = D("k")
m = B("m")

a.t = g
b.q = g
g.t = b
k.p = b
k.o = a
k.v = g
m.q = g
assert k.p.q.t.q.t.txt == "
class A:
    def __init__(self, txt: str):
        self.t: C = None
        self.txt = txt

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

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

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

a = A("a")
b = B("b")
g = C("g")
k = D("k")
m = B("m")

a.t = g
b.q = g
g.t = b
k.p = b
k.o = a
k.v = g
m.q = g
assert k.p.q.t.q.t.txt == "b"