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

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

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

j = A("j")
e = B("e")
d = C("d")
f = A("f")
g = B("g")
k = A("k")

j.q = d
j.w = d
e.x = j
e.y = k
e.w = g
d.r = f
f.q = d
f.w = d
g.x = k
g.y = f
g.w = e
k.q = d
k.w = d
assert d.r.w.r.q.r.w.txt == "
class A:
    def __init__(self, txt: str):
        self.q: C = None
        self.w: C = None
        self.txt = txt

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

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

j = A("j")
e = B("e")
d = C("d")
f = A("f")
g = B("g")
k = A("k")

j.q = d
j.w = d
e.x = j
e.y = k
e.w = g
d.r = f
f.q = d
f.w = d
g.x = k
g.y = f
g.w = e
k.q = d
k.w = d
assert d.r.w.r.q.r.w.txt == "d"