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

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

i = A("i")
b = B("b")
f = A("f")
d = B("d")

i.q = f
i.o = b
b.t = i
b.p = d
f.q = i
f.o = b
d.t = i
d.p = b
assert i.o.t.o.txt == "
class A:
    def __init__(self, txt: str):
        self.q: A = None
        self.o: B = None
        self.txt = txt

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

i = A("i")
b = B("b")
f = A("f")
d = B("d")

i.q = f
i.o = b
b.t = i
b.p = d
f.q = i
f.o = b
d.t = i
d.p = b
assert i.o.t.o.txt == "b"