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

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

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

a.q = m
d.w = m
j.w = h
i.q = h
h.q = a
m.q = i
g.w = m
b.w = i
assert a.q.q.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.w: A = None
        self.txt = txt

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

a.q = m
d.w = m
j.w = h
i.q = h
h.q = a
m.q = i
g.w = m
b.w = i
assert a.q.q.q.q.q.q.txt == "i"