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

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

f = A("f")
g = B("g")
m = A("m")
d = A("d")
n = B("n")
h = A("h")

f.u = m
g.x = d
g.r = m
m.u = d
d.u = h
n.x = m
n.r = d
h.u = m
assert h.u.u.u.txt == "
class A:
    def __init__(self, txt: str):
        self.u: A = None
        self.txt = txt

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

f = A("f")
g = B("g")
m = A("m")
d = A("d")
n = B("n")
h = A("h")

f.u = m
g.x = d
g.r = m
m.u = d
d.u = h
n.x = m
n.r = d
h.u = m
assert h.u.u.u.txt == "h"