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

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

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

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

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

g.s = a
g.z = d
a.q = j
a.p = j
a.t = g
j.p = g
d.w = j
f.q = j
f.p = j
f.t = g
assert j.p.z.w.p.txt == "
class A:
    def __init__(self, txt: str):
        self.s: B = None
        self.z: D = None
        self.txt = txt

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

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

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

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

g.s = a
g.z = d
a.q = j
a.p = j
a.t = g
j.p = g
d.w = j
f.q = j
f.p = j
f.t = g
assert j.p.z.w.p.txt == "g"