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

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

n = A("n")
h = B("h")
f = A("f")
m = A("m")

n.q = f
h.w = f
f.q = n
m.q = f
assert f.q.q.q.txt == "
class A:
    def __init__(self, txt: str):
        self.q: A = None
        self.txt = txt

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

n = A("n")
h = B("h")
f = A("f")
m = A("m")

n.q = f
h.w = f
f.q = n
m.q = f
assert f.q.q.q.txt == "n"