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

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

h = A("h")
j = B("j")
c = B("c")
e = B("e")
f = A("f")
b = B("b")
m = A("m")

h.p = f
j.q = c
c.q = b
e.q = c
f.p = m
b.q = c
m.p = h
assert b.q.q.q.txt == "
class A:
    def __init__(self, txt: str):
        self.p: A = None
        self.txt = txt

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

h = A("h")
j = B("j")
c = B("c")
e = B("e")
f = A("f")
b = B("b")
m = A("m")

h.p = f
j.q = c
c.q = b
e.q = c
f.p = m
b.q = c
m.p = h
assert b.q.q.q.txt == "c"