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

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

n = A("n")
g = B("g")
m = B("m")
d = B("d")
i = B("i")
j = A("j")
k = B("k")
e = A("e")

n.p = j
n.w = j
g.z = e
m.z = n
d.z = e
i.z = n
j.p = n
j.w = e
k.z = n
e.p = n
e.w = j
assert m.z.p.w.p.txt == "
class A:
    def __init__(self, txt: str):
        self.p: A = None
        self.w: A = None
        self.txt = txt

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

n = A("n")
g = B("g")
m = B("m")
d = B("d")
i = B("i")
j = A("j")
k = B("k")
e = A("e")

n.p = j
n.w = j
g.z = e
m.z = n
d.z = e
i.z = n
j.p = n
j.w = e
k.z = n
e.p = n
e.w = j
assert m.z.p.w.p.txt == "n"