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

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

d = A("d")
a = B("a")
c = B("c")
g = B("g")

d.w = a
a.z = g
c.z = g
g.z = c
assert a.z.z.z.z.txt == "
class A:
    def __init__(self, txt: str):
        self.w: B = None
        self.txt = txt

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

d = A("d")
a = B("a")
c = B("c")
g = B("g")

d.w = a
a.z = g
c.z = g
g.z = c
assert a.z.z.z.z.txt == "c"