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

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

m = A("m")
g = B("g")
j = A("j")

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

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

m = A("m")
g = B("g")
j = A("j")

m.r = g
m.q = g
g.z = m
j.r = g
j.q = g
assert j.q.z.q.txt == "g"