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

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

m = A("m")
g = B("g")
h = A("h")
f = B("f")
l = A("l")

m.w = l
m.q = g
g.r = f
g.s = l
h.w = l
h.q = g
f.r = g
f.s = m
l.w = m
l.q = g
assert l.q.s.w.txt == "
class A:
    def __init__(self, txt: str):
        self.w: A = None
        self.q: B = None
        self.txt = txt

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

m = A("m")
g = B("g")
h = A("h")
f = B("f")
l = A("l")

m.w = l
m.q = g
g.r = f
g.s = l
h.w = l
h.q = g
f.r = g
f.s = m
l.w = m
l.q = g
assert l.q.s.w.txt == "m"