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

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

h = A("h")
f = B("f")
g = B("g")
c = B("c")
a = A("a")
k = B("k")
d = B("d")

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

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

h = A("h")
f = B("f")
g = B("g")
c = B("c")
a = A("a")
k = B("k")
d = B("d")

h.v = a
f.r = a
g.r = h
c.r = h
a.v = h
k.r = a
d.r = a
assert d.r.v.v.v.txt == "h"