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

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

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

n.w = f
n.r = i
i.q = b
f.w = n
f.r = b
b.q = i
assert i.q.q.q.q.txt == "
class A:
    def __init__(self, txt: str):
        self.w: A = None
        self.r: B = None
        self.txt = txt

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

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

n.w = f
n.r = i
i.q = b
f.w = n
f.r = b
b.q = i
assert i.q.q.q.q.txt == "i"