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

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

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

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

n = A("n")
g = B("g")
b = C("b")
j = D("j")

n.o = j
n.r = j
g.x = j
b.o = j
b.p = j
b.w = g
b.q = g
j.s = n
assert g.x.s.r.s.txt == "
class A:
    def __init__(self, txt: str):
        self.o: D = None
        self.r: D = None
        self.txt = txt

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

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

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

n = A("n")
g = B("g")
b = C("b")
j = D("j")

n.o = j
n.r = j
g.x = j
b.o = j
b.p = j
b.w = g
b.q = g
j.s = n
assert g.x.s.r.s.txt == "n"