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

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

h = A("h")
g = B("g")
c = A("c")
b = B("b")
e = B("e")
f = A("f")
a = A("a")
m = B("m")

h.z = c
g.w = a
c.z = a
b.w = h
e.w = c
f.z = a
a.z = c
m.w = f
assert g.w.z.z.z.txt == "
class A:
    def __init__(self, txt: str):
        self.z: A = None
        self.txt = txt

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

h = A("h")
g = B("g")
c = A("c")
b = B("b")
e = B("e")
f = A("f")
a = A("a")
m = B("m")

h.z = c
g.w = a
c.z = a
b.w = h
e.w = c
f.z = a
a.z = c
m.w = f
assert g.w.z.z.z.txt == "c"