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

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

l = A("l")
h = B("h")
c = B("c")
f = B("f")
k = B("k")
i = A("i")
n = B("n")
g = A("g")
j = B("j")
a = B("a")

l.s = g
h.r = g
c.r = l
f.r = g
k.r = i
i.s = g
n.r = i
g.s = i
j.r = g
a.r = i
assert g.s.s.s.s.txt == "
class A:
    def __init__(self, txt: str):
        self.s: A = None
        self.txt = txt

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

l = A("l")
h = B("h")
c = B("c")
f = B("f")
k = B("k")
i = A("i")
n = B("n")
g = A("g")
j = B("j")
a = B("a")

l.s = g
h.r = g
c.r = l
f.r = g
k.r = i
i.s = g
n.r = i
g.s = i
j.r = g
a.r = i
assert g.s.s.s.s.txt == "g"