def load_trec_to_list(input_file, valid_qids, all_docids, topk):
    """
    Convert base retrieval scores to qid2docids & qid2docid2scores.
    """
    qid2docids, qid2docid2score = {}, {}
    with open(input_file, 'r', encoding='utf-8') as reader:
        for line in reader:
            line = line.strip('\n').split(' ')
            assert len(line) == 6
            qid, _, docid, rank, score, _ = line
            
            if int(rank) > topk:
                continue
            
            if qid not in valid_qids:
                continue
                
            if docid not in all_docids:
                continue
            
            # qid2did_score
            if qid not in qid2docids:
                qid2docids[qid] = set()
                qid2docids[qid].add(docid)
                qid2docid2score[qid] = {docid: score}
            else:
                qid2docids[qid].add(docid)
                qid2docid2score[qid][docid] = score
    return qid2docids, qid2docid2score