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

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

k = A("k")
i = B("i")
m = B("m")
d = A("d")
g = B("g")

k.q = m
k.t = d
i.p = d
i.x = k
m.p = d
m.x = k
d.q = m
d.t = k
g.p = d
g.x = d
assert k.t.t.q.x.txt == "
class A:
    def __init__(self, txt: str):
        self.q: B = None
        self.t: A = None
        self.txt = txt

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

k = A("k")
i = B("i")
m = B("m")
d = A("d")
g = B("g")

k.q = m
k.t = d
i.p = d
i.x = k
m.p = d
m.x = k
d.q = m
d.t = k
g.p = d
g.x = d
assert k.t.t.q.x.txt == "k"