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

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

k = A("k")
g = B("g")
j = A("j")
d = B("d")

k.t = d
g.q = k
g.r = d
j.t = g
d.q = j
d.r = g
assert g.r.q.t.r.txt == "
class A:
    def __init__(self, txt: str):
        self.t: B = None
        self.txt = txt

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

k = A("k")
g = B("g")
j = A("j")
d = B("d")

k.t = d
g.q = k
g.r = d
j.t = g
d.q = j
d.r = g
assert g.r.q.t.r.txt == "d"