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

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

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

f = A("f")
d = B("d")
l = C("l")
i = B("i")
g = B("g")

f.x = g
f.z = g
d.p = f
d.s = l
d.w = l
l.q = f
i.p = f
i.s = l
i.w = l
g.p = f
g.s = l
g.w = l
assert i.p.z.p.txt == "
class A:
    def __init__(self, txt: str):
        self.x: B = None
        self.z: B = None
        self.txt = txt

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

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

f = A("f")
d = B("d")
l = C("l")
i = B("i")
g = B("g")

f.x = g
f.z = g
d.p = f
d.s = l
d.w = l
l.q = f
i.p = f
i.s = l
i.w = l
g.p = f
g.s = l
g.w = l
assert i.p.z.p.txt == "f"