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

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

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

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

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

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

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