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

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

d = A("d")
f = B("f")
a = B("a")
g = B("g")
j = B("j")
h = A("h")
k = B("k")

d.p = k
f.z = d
a.z = d
g.z = h
j.z = d
h.p = j
k.z = h
assert g.z.p.z.p.z.txt == "
class A:
    def __init__(self, txt: str):
        self.p: B = None
        self.txt = txt

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

d = A("d")
f = B("f")
a = B("a")
g = B("g")
j = B("j")
h = A("h")
k = B("k")

d.p = k
f.z = d
a.z = d
g.z = h
j.z = d
h.p = j
k.z = h
assert g.z.p.z.p.z.txt == "h"