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

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

f = A("f")
b = B("b")
h = A("h")

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

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

f = A("f")
b = B("b")
h = A("h")

f.q = h
f.v = h
b.p = h
b.u = f
h.q = f
h.v = f
assert h.q.v.v.txt == "f"