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

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

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

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

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

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

n.q = e
f.p = h
f.w = h
e.q = n
h.p = f
h.w = f
assert f.w.p.p.w.txt == "f"