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

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

h = A("h")
g = B("g")
a = B("a")
b = A("b")
f = A("f")
m = B("m")

h.t = f
g.o = m
g.q = m
a.o = g
a.q = g
b.t = h
f.t = h
m.o = g
m.q = g
assert a.q.o.o.q.txt == "
class A:
    def __init__(self, txt: str):
        self.t: A = None
        self.txt = txt

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

h = A("h")
g = B("g")
a = B("a")
b = A("b")
f = A("f")
m = B("m")

h.t = f
g.o = m
g.q = m
a.o = g
a.q = g
b.t = h
f.t = h
m.o = g
m.q = g
assert a.q.o.o.q.txt == "m"