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

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

m = A("m")
b = B("b")
h = B("h")
k = B("k")
d = B("d")

m.r = k
b.q = k
b.w = d
h.q = b
h.w = d
k.q = h
k.w = h
d.q = h
d.w = b
assert k.w.q.q.txt == "
class A:
    def __init__(self, txt: str):
        self.r: B = None
        self.txt = txt

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

m = A("m")
b = B("b")
h = B("h")
k = B("k")
d = B("d")

m.r = k
b.q = k
b.w = d
h.q = b
h.w = d
k.q = h
k.w = h
d.q = h
d.w = b
assert k.w.q.q.txt == "k"