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

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

e = A("e")
a = B("a")
d = B("d")
h = A("h")

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

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

e = A("e")
a = B("a")
d = B("d")
h = A("h")

e.r = h
a.w = d
d.w = a
h.r = e
assert e.r.r.r.txt == "h"