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

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

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

h.r = b
m.x = h
m.q = a
c.x = h
c.q = k
b.r = h
a.x = b
a.q = k
l.x = b
l.q = c
k.x = h
k.q = a
assert m.q.x.r.r.r.r.txt == "
class A:
    def __init__(self, txt: str):
        self.r: A = None
        self.txt = txt

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

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

h.r = b
m.x = h
m.q = a
c.x = h
c.q = k
b.r = h
a.x = b
a.q = k
l.x = b
l.q = c
k.x = h
k.q = a
assert m.q.x.r.r.r.r.txt == "b"