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

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

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

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

j = A("j")
g = B("g")
k = C("k")
m = D("m")

j.p = g
g.r = k
g.z = j
k.y = m
k.q = m
m.r = j
assert m.r.p.r.txt == "
class A:
    def __init__(self, txt: str):
        self.p: B = None
        self.txt = txt

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

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

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

j = A("j")
g = B("g")
k = C("k")
m = D("m")

j.p = g
g.r = k
g.z = j
k.y = m
k.q = m
m.r = j
assert m.r.p.r.txt == "k"