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

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

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

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

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

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

d.r = n
a.z = h
a.s = g
h.r = a
i.r = n
g.z = i
g.s = n
n.z = d
n.s = g
assert g.z.r.s.txt == "g"