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

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

a = A("a")
d = B("d")
i = B("i")
k = B("k")
g = A("g")
m = A("m")

a.q = d
a.u = k
d.v = k
i.v = d
k.v = i
g.q = d
g.u = d
m.q = i
m.u = i
assert m.q.v.v.v.v.txt == "
class A:
    def __init__(self, txt: str):
        self.q: B = None
        self.u: B = None
        self.txt = txt

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

a = A("a")
d = B("d")
i = B("i")
k = B("k")
g = A("g")
m = A("m")

a.q = d
a.u = k
d.v = k
i.v = d
k.v = i
g.q = d
g.u = d
m.q = i
m.u = i
assert m.q.v.v.v.v.txt == "d"