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

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

a = A("a")
g = B("g")
d = A("d")
f = A("f")
k = A("k")
b = B("b")
l = B("l")
i = B("i")

a.q = d
g.r = a
g.t = f
d.q = k
f.q = d
k.q = a
b.r = a
b.t = k
l.r = f
l.t = f
i.r = d
i.t = d
assert f.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.r: A = None
        self.t: A = None
        self.txt = txt

a = A("a")
g = B("g")
d = A("d")
f = A("f")
k = A("k")
b = B("b")
l = B("l")
i = B("i")

a.q = d
g.r = a
g.t = f
d.q = k
f.q = d
k.q = a
b.r = a
b.t = k
l.r = f
l.t = f
i.r = d
i.t = d
assert f.q.q.q.q.txt == "d"