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

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

f = A("f")
j = B("j")
g = A("g")
a = A("a")
b = A("b")
m = B("m")

f.r = m
j.w = g
j.z = m
g.r = m
a.r = j
b.r = j
m.w = f
m.z = j
assert m.w.r.z.txt == "
class A:
    def __init__(self, txt: str):
        self.r: B = None
        self.txt = txt

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

f = A("f")
j = B("j")
g = A("g")
a = A("a")
b = A("b")
m = B("m")

f.r = m
j.w = g
j.z = m
g.r = m
a.r = j
b.r = j
m.w = f
m.z = j
assert m.w.r.z.txt == "j"