Time limit exceeded for Sherlock and Queries Hackerrank challenge
The time complexity of your solution is 
\$ O(NM) \$. It is too slow. You need a more efficient algorithm. Here is a pseudo code of a better one:

// Reads the input.
n = read_input()
m = read_input()
a = read_input()
b = read_input()
c = read_input()
// Creates an auxilary array to remove duplicates from b 
totalProduct = an array of n + 1 elements filled with 1
for (i = 1; i &lt;= m; i++)
    if (b[i] &lt;= n)
        totalProduct[b[i]] = totalProduct[b[i]] * c[i] % MOD
for (divisor = 1; divisor &lt;= n; divisor++)
    // Iterates over all number that are divisible by the divisor
    for (number = divisor; number &lt;= n; number += divisor)
        a[number] = a[number] * totalProduct[divisor] % MOD
print(a)


The correctness of this algorithm is obvious (it simply does what is written in the problem statement). So why is it efficient? It is fast because its time complexity is $$ O\big(M + { N \over 1 } + { N \over 2 } + { N \over 3 } + \dotsb + { N \over N }\big) = O(M + N \log N). $$ 
Why do we need to remove duplicates from b? If we don't do it, the time complexity will be \$ O(M + N^2) \$ if all elements of b are equal to 1.