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

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

a = A("a")
n = B("n")
m = B("m")

a.q = n
n.o = m
m.o = n
assert n.o.o.o.txt == "
class A:
    def __init__(self, txt: str):
        self.q: B = None
        self.txt = txt

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

a = A("a")
n = B("n")
m = B("m")

a.q = n
n.o = m
m.o = n
assert n.o.o.o.txt == "m"