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

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

a = A("a")
n = B("n")
d = A("d")
i = A("i")
m = B("m")
h = B("h")
b = A("b")
f = A("f")

a.r = i
a.w = d
n.v = d
d.r = b
d.w = a
i.r = f
i.w = a
m.v = i
h.v = b
b.r = i
b.w = a
f.r = a
f.w = i
assert d.r.w.r.r.txt == "
class A:
    def __init__(self, txt: str):
        self.r: A = None
        self.w: A = None
        self.txt = txt

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

a = A("a")
n = B("n")
d = A("d")
i = A("i")
m = B("m")
h = B("h")
b = A("b")
f = A("f")

a.r = i
a.w = d
n.v = d
d.r = b
d.w = a
i.r = f
i.w = a
m.v = i
h.v = b
b.r = i
b.w = a
f.r = a
f.w = i
assert d.r.w.r.r.txt == "f"