Since ai is rather small, we can precalculate all the factors of all the numbers smaller than 1e5. Then, for each factor, we store all the i such that ai contains this factor in ascending order.
For each query, we iterate all the factors from biggest to smallest and see if we can find some number in [l,r] that contains this factor. We could use binary search to achieve this.