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

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

h = A("h")
m = B("m")
k = A("k")
f = B("f")
n = A("n")
j = B("j")

h.o = k
h.p = m
m.q = k
k.o = h
k.p = m
f.q = h
n.o = h
n.p = f
j.q = k
assert k.p.q.p.txt == "
class A:
    def __init__(self, txt: str):
        self.o: A = None
        self.p: B = None
        self.txt = txt

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

h = A("h")
m = B("m")
k = A("k")
f = B("f")
n = A("n")
j = B("j")

h.o = k
h.p = m
m.q = k
k.o = h
k.p = m
f.q = h
n.o = h
n.p = f
j.q = k
assert k.p.q.p.txt == "m"