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

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

h = A("h")
d = B("d")
b = A("b")

h.w = b
d.w = b
d.u = h
b.w = h
assert d.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.w: A = None
        self.u: A = None
        self.txt = txt

h = A("h")
d = B("d")
b = A("b")

h.w = b
d.w = b
d.u = h
b.w = h
assert d.w.w.w.txt == "b"