from collections import defaultdict


def preprocess_synonyms(synonym_words):
    synonyms = defaultdict(set)
    for w1, w2 in synonym_words:
        for w in synonyms[w1]:
            synonyms[w].add(w2)
        synonyms[w1].add(w2)
        for w in synonyms[w2]:
            synonyms[w].add(w1)
        synonyms[w2].add(w1)
    return synonyms


def synonym_queries(synonym_words, queries):
    '''
    synonym_words: iterable of pairs of strings representing synonymous words
    queries: iterable of pairs of strings representing queries to be tested for 
             synonymous-ness
    '''
    synonyms = preprocess_synonyms(synonym_words)

    output = []
    for q1, q2 in queries:
        q1, q2 = q1.split(), q2.split()
        if len(q1) != len(q2):
            output.append(False)
            continue
        result = True
        for i in range(len(q1)):
            w1, w2 = q1[i], q2[i]
            if w1 == w2:
                continue
            elif ((w1 in synonyms and w2 in synonyms[w1]) 
                    or (w2 in synonyms and w1 in synonyms[w2])):
                continue
            result = False
            break
        output.append(result)
    return output


if __name__ == '__main__':
    import console
    console.console(synonym_queries)
