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

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

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

g.v = m
h.q = m
h.p = m
m.v = g
assert m.v.v.v.txt == "
class A:
    def __init__(self, txt: str):
        self.v: A = None
        self.txt = txt

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

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

g.v = m
h.q = m
h.p = m
m.v = g
assert m.v.v.v.txt == "g"