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

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

d = A("d")
g = B("g")
j = A("j")

d.r = j
g.w = d
j.r = d
assert g.w.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: A = None
        self.txt = txt

d = A("d")
g = B("g")
j = A("j")

d.r = j
g.w = d
j.r = d
assert g.w.r.r.txt == "d"