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

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

d = A("d")
a = B("a")
g = A("g")
n = B("n")
m = B("m")
h = B("h")
j = B("j")

d.q = g
a.t = m
a.r = g
g.q = d
n.t = m
n.r = d
m.t = a
m.r = d
h.t = j
h.r = g
j.t = h
j.r = g
assert j.t.r.q.q.q.q.txt == "
class A:
    def __init__(self, txt: str):
        self.q: A = None
        self.txt = txt

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

d = A("d")
a = B("a")
g = A("g")
n = B("n")
m = B("m")
h = B("h")
j = B("j")

d.q = g
a.t = m
a.r = g
g.q = d
n.t = m
n.r = d
m.t = a
m.r = d
h.t = j
h.r = g
j.t = h
j.r = g
assert j.t.r.q.q.q.q.txt == "g"