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

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

g = A("g")
a = B("a")
h = A("h")
e = B("e")
j = B("j")
n = A("n")

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

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

g = A("g")
a = B("a")
h = A("h")
e = B("e")
j = B("j")
n = A("n")

g.p = h
g.w = j
a.x = g
h.p = n
h.w = a
e.x = h
j.x = n
n.p = h
n.w = e
assert j.x.p.w.x.w.txt == "j"