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

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

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

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

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

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

n.p = f
n.q = m
j.x = h
j.w = k
k.x = i
k.w = j
h.p = f
h.q = i
i.p = h
i.q = h
m.p = i
m.q = n
f.p = m
f.q = h
assert h.p.q.q.p.q.txt == "i"