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

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

a = A("a")
n = B("n")
g = A("g")
e = B("e")

a.r = g
n.x = a
g.r = a
e.x = g
assert e.x.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.x: A = None
        self.txt = txt

a = A("a")
n = B("n")
g = A("g")
e = B("e")

a.r = g
n.x = a
g.r = a
e.x = g
assert e.x.r.r.r.txt == "a"