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

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

n = A("n")
i = B("i")
g = B("g")

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

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

n = A("n")
i = B("i")
g = B("g")

n.q = i
i.p = g
i.q = g
g.p = i
g.q = i
assert n.q.p.p.txt == "i"