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

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

h = A("h")
m = B("m")
c = B("c")
b = A("b")
g = B("g")
n = B("n")
l = A("l")
k = B("k")

h.x = m
m.r = n
c.r = n
b.x = m
g.r = k
n.r = m
l.x = c
k.r = g
assert h.x.r.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: B = None
        self.txt = txt

h = A("h")
m = B("m")
c = B("c")
b = A("b")
g = B("g")
n = B("n")
l = A("l")
k = B("k")

h.x = m
m.r = n
c.r = n
b.x = m
g.r = k
n.r = m
l.x = c
k.r = g
assert h.x.r.r.txt == "m"