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

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

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

m.v = h
h.x = i
h.r = m
i.v = h
assert i.v.x.v.txt == "
class A:
    def __init__(self, txt: str):
        self.v: B = None
        self.txt = txt

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

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

m.v = h
h.x = i
h.r = m
i.v = h
assert i.v.x.v.txt == "h"