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

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

f = A("f")
g = B("g")
b = B("b")
i = A("i")
h = A("h")
d = B("d")

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

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

f = A("f")
g = B("g")
b = B("b")
i = A("i")
h = A("h")
d = B("d")

f.u = b
g.w = i
g.r = d
b.w = h
b.r = d
i.u = b
h.u = d
d.w = h
d.r = g
assert d.w.u.r.r.r.txt == "g"