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

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

n = A("n")
a = B("a")
f = A("f")
j = B("j")
g = A("g")

n.u = f
n.p = f
a.q = j
f.u = g
f.p = g
j.q = a
g.u = n
g.p = f
assert j.q.q.q.txt == "
class A:
    def __init__(self, txt: str):
        self.u: A = None
        self.p: A = None
        self.txt = txt

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

n = A("n")
a = B("a")
f = A("f")
j = B("j")
g = A("g")

n.u = f
n.p = f
a.q = j
f.u = g
f.p = g
j.q = a
g.u = n
g.p = f
assert j.q.q.q.txt == "a"