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

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

b = A("b")
h = B("h")
j = A("j")
d = B("d")
m = B("m")
i = A("i")

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

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

b = A("b")
h = B("h")
j = A("j")
d = B("d")
m = B("m")
i = A("i")

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