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

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

g = A("g")
m = B("m")
a = B("a")
h = B("h")
i = A("i")

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

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

g = A("g")
m = B("m")
a = B("a")
h = B("h")
i = A("i")

g.u = i
m.q = a
m.o = g
a.q = h
a.o = i
h.q = m
h.o = g
i.u = g
assert m.q.q.o.txt == "g"