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

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

n = A("n")
d = B("d")
e = B("e")
m = B("m")
b = A("b")

n.s = b
d.q = m
e.q = d
m.q = e
b.s = n
assert b.s.s.s.txt == "
class A:
    def __init__(self, txt: str):
        self.s: A = None
        self.txt = txt

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

n = A("n")
d = B("d")
e = B("e")
m = B("m")
b = A("b")

n.s = b
d.q = m
e.q = d
m.q = e
b.s = n
assert b.s.s.s.txt == "n"