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

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

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

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

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

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

g.z = k
f.u = h
f.x = a
k.u = h
k.x = f
h.z = k
a.u = g
a.x = f
assert a.u.z.u.z.txt == "k"