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

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

d = A("d")
b = B("b")
k = B("k")
g = A("g")
h = B("h")

d.u = h
b.r = h
b.z = g
k.r = h
k.z = d
g.u = b
h.r = b
h.z = g
assert d.u.z.u.txt == "
class A:
    def __init__(self, txt: str):
        self.u: B = None
        self.txt = txt

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

d = A("d")
b = B("b")
k = B("k")
g = A("g")
h = B("h")

d.u = h
b.r = h
b.z = g
k.r = h
k.z = d
g.u = b
h.r = b
h.z = g
assert d.u.z.u.txt == "b"