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

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

m = A("m")
n = B("n")
j = B("j")
d = B("d")
h = B("h")
e = A("e")
f = A("f")
a = A("a")

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

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

m = A("m")
n = B("n")
j = B("j")
d = B("d")
h = B("h")
e = A("e")
f = A("f")
a = A("a")

m.s = h
n.r = j
j.r = d
d.r = j
h.r = d
e.s = h
f.s = h
a.s = j
assert f.s.r.r.txt == "j"