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

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

a = A("a")
k = B("k")
m = B("m")
e = A("e")
b = A("b")
g = A("g")
h = B("h")

a.q = m
a.r = e
k.u = h
m.u = h
e.q = h
e.r = b
b.q = h
b.r = e
g.q = k
g.r = b
h.u = k
assert k.u.u.u.u.txt == "
class A:
    def __init__(self, txt: str):
        self.q: B = None
        self.r: A = None
        self.txt = txt

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

a = A("a")
k = B("k")
m = B("m")
e = A("e")
b = A("b")
g = A("g")
h = B("h")

a.q = m
a.r = e
k.u = h
m.u = h
e.q = h
e.r = b
b.q = h
b.r = e
g.q = k
g.r = b
h.u = k
assert k.u.u.u.u.txt == "k"