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

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

i = A("i")
c = B("c")
h = A("h")
a = A("a")

i.w = h
c.r = a
c.z = h
h.w = i
a.w = i
assert a.w.w.w.w.txt == "
class A:
    def __init__(self, txt: str):
        self.w: A = None
        self.txt = txt

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

i = A("i")
c = B("c")
h = A("h")
a = A("a")

i.w = h
c.r = a
c.z = h
h.w = i
a.w = i
assert a.w.w.w.w.txt == "h"