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

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

a = A("a")
f = B("f")
k = B("k")
n = A("n")
d = B("d")
i = A("i")
m = B("m")
g = A("g")

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

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

a = A("a")
f = B("f")
k = B("k")
n = A("n")
d = B("d")
i = A("i")
m = B("m")
g = A("g")

a.q = i
a.w = n
f.p = g
k.p = n
n.q = i
n.w = a
d.p = g
i.q = g
i.w = a
m.p = i
g.q = a
g.w = n
assert g.q.w.q.txt == "i"