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

class B:
    def __init__(self, txt: str):
        self.w: A = None
        self.t: D = None
        self.x: C = None
        self.q: C = None
        self.txt = txt

class C:
    def __init__(self, txt: str):
        self.y: B = None
        self.txt = txt

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

h = A("h")
m = B("m")
c = C("c")
n = D("n")
d = A("d")

h.r = d
m.w = d
m.t = n
m.x = c
m.q = c
c.y = m
n.s = m
n.q = m
n.y = h
d.r = h
assert h.r.r.r.txt == "
class A:
    def __init__(self, txt: str):
        self.r: A = None
        self.txt = txt

class B:
    def __init__(self, txt: str):
        self.w: A = None
        self.t: D = None
        self.x: C = None
        self.q: C = None
        self.txt = txt

class C:
    def __init__(self, txt: str):
        self.y: B = None
        self.txt = txt

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

h = A("h")
m = B("m")
c = C("c")
n = D("n")
d = A("d")

h.r = d
m.w = d
m.t = n
m.x = c
m.q = c
c.y = m
n.s = m
n.q = m
n.y = h
d.r = h
assert h.r.r.r.txt == "d"