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

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

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

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

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

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

e.r = h
e.q = m
f.z = m
f.r = e
g.r = e
g.q = f
m.z = f
m.r = h
h.r = g
h.q = f
assert m.r.q.z.txt == "m"