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

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

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

j.y = d
j.w = g
g.r = d
d.y = j
d.w = g
assert g.r.w.r.txt == "
class A:
    def __init__(self, txt: str):
        self.y: A = None
        self.w: B = None
        self.txt = txt

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

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

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