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

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

n = A("n")
i = B("i")
d = A("d")
h = A("h")
g = A("g")
c = B("c")
a = A("a")

n.x = i
i.z = c
d.x = c
h.x = c
g.x = i
c.z = i
a.x = i
assert g.x.z.z.z.z.txt == "
class A:
    def __init__(self, txt: str):
        self.x: B = None
        self.txt = txt

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

n = A("n")
i = B("i")
d = A("d")
h = A("h")
g = A("g")
c = B("c")
a = A("a")

n.x = i
i.z = c
d.x = c
h.x = c
g.x = i
c.z = i
a.x = i
assert g.x.z.z.z.z.txt == "i"