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

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

g = A("g")
l = B("l")
c = A("c")
b = B("b")
h = A("h")
a = A("a")
d = B("d")

g.x = b
l.r = a
c.x = b
b.r = a
h.x = b
a.x = b
d.r = a
assert d.r.x.r.x.r.txt == "
class A:
    def __init__(self, txt: str):
        self.x: B = None
        self.txt = txt

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

g = A("g")
l = B("l")
c = A("c")
b = B("b")
h = A("h")
a = A("a")
d = B("d")

g.x = b
l.r = a
c.x = b
b.r = a
h.x = b
a.x = b
d.r = a
assert d.r.x.r.x.r.txt == "a"