def spell_check(T, w):
    """Spellchecker

    :param T: trie encoding the dictionary
    :param w: given word
    :returns: a closest word from the dictionary
    :complexity: linear if distance was constant
    """
    assert T is not None
    dist = 0
    while True:   # Try increasing distances
        u = search(T, dist, w)
        if u is not None:
            return u
        dist += 1