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

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

a = A("a")
d = B("d")
h = B("h")
b = B("b")
k = A("k")
f = A("f")

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

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

a = A("a")
d = B("d")
h = B("h")
b = B("b")
k = A("k")
f = A("f")

a.v = k
a.r = f
d.w = f
h.w = a
b.w = a
k.v = a
k.r = f
f.v = k
f.r = k
assert f.r.r.r.v.txt == "a"