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

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

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

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

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

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

h.p = n
a.q = n
a.p = n
n.p = h
assert a.p.p.p.txt == "n"