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

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

f = A("f")
j = B("j")
m = B("m")
c = A("c")

f.o = c
f.q = c
j.t = m
j.q = f
m.t = j
m.q = c
c.o = f
c.q = f
assert j.t.t.t.q.txt == "
class A:
    def __init__(self, txt: str):
        self.o: A = None
        self.q: A = None
        self.txt = txt

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

f = A("f")
j = B("j")
m = B("m")
c = A("c")

f.o = c
f.q = c
j.t = m
j.q = f
m.t = j
m.q = c
c.o = f
c.q = f
assert j.t.t.t.q.txt == "c"