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

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

i = A("i")
g = B("g")
n = B("n")
h = A("h")
d = A("d")
l = A("l")
m = B("m")
j = A("j")

i.p = g
g.w = i
g.x = n
n.w = h
n.x = g
h.p = n
d.p = g
l.p = g
m.w = i
m.x = g
j.p = n
assert g.x.w.p.txt == "
class A:
    def __init__(self, txt: str):
        self.p: B = None
        self.txt = txt

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

i = A("i")
g = B("g")
n = B("n")
h = A("h")
d = A("d")
l = A("l")
m = B("m")
j = A("j")

i.p = g
g.w = i
g.x = n
n.w = h
n.x = g
h.p = n
d.p = g
l.p = g
m.w = i
m.x = g
j.p = n
assert g.x.w.p.txt == "n"