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

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

n = A("n")
a = B("a")
j = A("j")
i = B("i")

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

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

n = A("n")
a = B("a")
j = A("j")
i = B("i")

n.p = a
a.q = i
a.p = i
j.p = i
i.q = a
i.p = a
assert i.q.p.p.txt == "a"