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

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

class C:
    def __init__(self, txt: str):
        self.p: D = None
        self.txt = txt

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

h = A("h")
k = B("k")
m = C("m")
f = D("f")
d = A("d")
i = A("i")

h.w = k
h.r = i
k.r = f
k.x = f
m.p = f
f.y = h
d.w = k
d.r = h
i.w = k
i.r = d
assert f.y.w.r.y.txt == "
class A:
    def __init__(self, txt: str):
        self.w: B = None
        self.r: A = None
        self.txt = txt

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

class C:
    def __init__(self, txt: str):
        self.p: D = None
        self.txt = txt

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

h = A("h")
k = B("k")
m = C("m")
f = D("f")
d = A("d")
i = A("i")

h.w = k
h.r = i
k.r = f
k.x = f
m.p = f
f.y = h
d.w = k
d.r = h
i.w = k
i.r = d
assert f.y.w.r.y.txt == "h"