private List<Predicate> substituteQuery(List<UnificationPair> mgu, List<Predicate> query) {
        ListIterator<Predicate> predicates = query.listIterator();
        while (predicates.hasNext()) {
            Predicate predicate = predicates.next();
            for (UnificationPair termPair : mgu) {
                predicate = (Predicate) predicate.substitute(
                        termPair.getLhs(), termPair.getRhs());
            }

            predicates.set(predicate);
        }

        return query;
    }