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

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

h = A("h")
d = B("d")
j = A("j")
k = A("k")

h.x = j
h.w = d
d.p = k
j.x = h
j.w = d
k.x = h
k.w = d
assert k.w.p.w.p.txt == "
class A:
    def __init__(self, txt: str):
        self.x: A = None
        self.w: B = None
        self.txt = txt

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

h = A("h")
d = B("d")
j = A("j")
k = A("k")

h.x = j
h.w = d
d.p = k
j.x = h
j.w = d
k.x = h
k.w = d
assert k.w.p.w.p.txt == "k"