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

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

j = A("j")
e = B("e")
a = A("a")
b = A("b")
f = A("f")
h = B("h")
g = B("g")
d = A("d")

j.r = g
e.s = h
a.r = e
b.r = e
f.r = g
h.s = g
g.s = h
d.r = e
assert a.r.s.s.txt == "
class A:
    def __init__(self, txt: str):
        self.r: B = None
        self.txt = txt

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

j = A("j")
e = B("e")
a = A("a")
b = A("b")
f = A("f")
h = B("h")
g = B("g")
d = A("d")

j.r = g
e.s = h
a.r = e
b.r = e
f.r = g
h.s = g
g.s = h
d.r = e
assert a.r.s.s.txt == "g"