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

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

m = A("m")
g = B("g")
e = A("e")
h = B("h")

m.r = e
m.q = h
g.u = e
e.r = m
e.q = h
h.u = m
assert m.q.u.q.txt == "
class A:
    def __init__(self, txt: str):
        self.r: A = None
        self.q: B = None
        self.txt = txt

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

m = A("m")
g = B("g")
e = A("e")
h = B("h")

m.r = e
m.q = h
g.u = e
e.r = m
e.q = h
h.u = m
assert m.q.u.q.txt == "h"