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

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

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

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

f.q = e
f.s = k
d.q = k
d.p = m
k.r = d
h.q = f
h.s = k
e.q = g
e.s = k
g.q = f
g.s = k
m.q = k
m.p = d
j.q = g
j.s = k
assert j.s.r.p.p.p.p.q.r.txt == "
class A:
    def __init__(self, txt: str):
        self.q: A = None
        self.s: C = None
        self.txt = txt

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

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

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

f.q = e
f.s = k
d.q = k
d.p = m
k.r = d
h.q = f
h.s = k
e.q = g
e.s = k
g.q = f
g.s = k
m.q = k
m.p = d
j.q = g
j.s = k
assert j.s.r.p.p.p.p.q.r.txt == "d"