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

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

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

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

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

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

a.u = g
d.r = g
g.r = d
assert d.r.r.r.txt == "g"