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

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

c = A("c")
n = B("n")
a = B("a")
b = B("b")
d = A("d")

c.z = a
n.p = c
n.q = d
a.p = c
a.q = d
b.p = d
b.q = c
d.z = n
assert b.q.z.q.txt == "
class A:
    def __init__(self, txt: str):
        self.z: B = None
        self.txt = txt

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

c = A("c")
n = B("n")
a = B("a")
b = B("b")
d = A("d")

c.z = a
n.p = c
n.q = d
a.p = c
a.q = d
b.p = d
b.q = c
d.z = n
assert b.q.z.q.txt == "d"