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

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

f = A("f")
h = B("h")
i = B("i")
j = A("j")

f.x = i
f.p = h
h.w = j
h.q = f
i.w = f
i.q = j
j.x = i
j.p = i
assert h.q.p.q.x.txt == "
class A:
    def __init__(self, txt: str):
        self.x: B = None
        self.p: B = None
        self.txt = txt

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

f = A("f")
h = B("h")
i = B("i")
j = A("j")

f.x = i
f.p = h
h.w = j
h.q = f
i.w = f
i.q = j
j.x = i
j.p = i
assert h.q.p.q.x.txt == "i"