#include <bits/stdc++.h>
using namespace std;
#define pb emplace_back
#define fo(i, n) for(int i = 1; i <= n; ++i)
typedef long long ll;
const int N = 1000300;
const int mod = 1e9 + 7;
int lp[N];
int l[200200], r[200200];
int a[200200], n, q;
int ans[200200];
int cnt[N];
int X[N];
vector<int> e[200200];
inline void sieve() {
for(int i = 2; i <= 1000000; ++i) {
if(!lp[i]) {
lp[i] = i;
if(i * 1ll * i <= 1000000)
for(int j = i * i; j <= 1000000; j += i)
lp[j] = i;
}
}
}
vector<pair<int, int> > primes;
vector<int> divi, pr;
inline void calc(int p = 0, int x = 1) {
if(p == primes.size()) {
divi.pb(x);
return;
}
calc(p + 1, x);
for(int i = 0; i < primes[p].second; ++i) {
x *= primes[p].first;
calc(p + 1, x);
}
}
inline void gen_divisors(int n)
{
primes.clear();
divi.clear();
while(n > 1)
{
int x = lp[n], st = 0;
while(n % x == 0) n /= x, st++;
primes.pb(x, st);
}
calc();
}
int sign, cur_id, s2;
inline void gen_masks(int p = 0, int x = 1, int c = 0) {
if(p == pr.size()) {
if(c) s2 = -1;
else s2 = 1;
ans[cur_id] += cnt[x] * s2 * sign;
return ;
}
gen_masks(p + 1, x * pr[p], c ^ 1);
gen_masks(p + 1, x, c);
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
sieve();
cin >> n;
fo(i, n) {
cin >> a[i];
}
cin >> q;
fo(it, q) {
cin >> l[it] >> r[it] >> X[it];
e[l[it]-1].pb(-it);
e[r[it]].pb(it);
}
fo(i, n) {
gen_divisors(a[i]);
for(int j : divi) cnt[j]++;
for(int t = 0; t < e[i].size(); ++t) {
int id = e[i][t], x = X[abs(id)];
pr.clear();
while(x > 1) {
int y = lp[x];
pr.pb(y);
while(x % y == 0) x /= y;
}
sign = (id < 0 ? -1 : 1), cur_id = abs(id);
gen_masks();
}
}
fo(it, q) cout << ans[it] << '\n';
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKI2RlZmluZSBwYiBlbXBsYWNlX2JhY2sKI2RlZmluZSBmbyhpLCBuKSBmb3IoaW50IGkgPSAxOyBpIDw9IG47ICsraSkKCnR5cGVkZWYgbG9uZyBsb25nIGxsOwoKY29uc3QgaW50IE4gPSAxMDAwMzAwOwpjb25zdCBpbnQgbW9kID0gMWU5ICsgNzsKCmludCBscFtOXTsKaW50IGxbMjAwMjAwXSwgclsyMDAyMDBdOwppbnQgYVsyMDAyMDBdLCBuLCBxOwppbnQgYW5zWzIwMDIwMF07CmludCBjbnRbTl07CmludCBYW05dOwp2ZWN0b3I8aW50PiBlWzIwMDIwMF07CgppbmxpbmUgdm9pZCBzaWV2ZSgpIHsKICAgIGZvcihpbnQgaSA9IDI7IGkgPD0gMTAwMDAwMDsgKytpKSB7CiAgICAgICAgaWYoIWxwW2ldKSB7CiAgICAgICAgICAgIGxwW2ldID0gaTsKICAgICAgICAgICAgaWYoaSAqIDFsbCAqIGkgPD0gMTAwMDAwMCkKICAgICAgICAgICAgICAgIGZvcihpbnQgaiA9IGkgKiBpOyBqIDw9IDEwMDAwMDA7IGogKz0gaSkKICAgICAgICAgICAgICAgICAgICBscFtqXSA9IGk7CiAgICAgICAgfQogICAgfQp9CnZlY3RvcjxwYWlyPGludCwgaW50PiA+IHByaW1lczsKdmVjdG9yPGludD4gZGl2aSwgcHI7CgppbmxpbmUgdm9pZCBjYWxjKGludCBwID0gMCwgaW50IHggPSAxKSB7CiAgICBpZihwID09IHByaW1lcy5zaXplKCkpIHsKICAgICAgICBkaXZpLnBiKHgpOwogICAgICAgIHJldHVybjsKICAgIH0KICAgIGNhbGMocCArIDEsIHgpOwogICAgZm9yKGludCBpID0gMDsgaSA8IHByaW1lc1twXS5zZWNvbmQ7ICsraSkgewogICAgICAgIHggKj0gcHJpbWVzW3BdLmZpcnN0OwogICAgICAgIGNhbGMocCArIDEsIHgpOwogICAgfQp9CgppbmxpbmUgdm9pZCBnZW5fZGl2aXNvcnMoaW50IG4pCnsKICAgIHByaW1lcy5jbGVhcigpOwogICAgZGl2aS5jbGVhcigpOwogICAgd2hpbGUobiA+IDEpCiAgICB7CiAgICAgICAgaW50IHggPSBscFtuXSwgc3QgPSAwOwogICAgICAgIHdoaWxlKG4gJSB4ID09IDApIG4gLz0geCwgc3QrKzsKICAgICAgICBwcmltZXMucGIoeCwgc3QpOwogICAgfQogICAgY2FsYygpOwp9CgppbnQgc2lnbiwgY3VyX2lkLCBzMjsKCmlubGluZSB2b2lkIGdlbl9tYXNrcyhpbnQgcCA9IDAsIGludCB4ID0gMSwgaW50IGMgPSAwKSB7CiAgICBpZihwID09IHByLnNpemUoKSkgewogICAgICAgIGlmKGMpIHMyID0gLTE7CiAgICAgICAgZWxzZSBzMiA9IDE7CiAgICAgICAgYW5zW2N1cl9pZF0gKz0gY250W3hdICogczIgKiBzaWduOwogICAgICAgIHJldHVybiA7CiAgICB9CiAgICBnZW5fbWFza3MocCArIDEsIHggKiBwcltwXSwgYyBeIDEpOwogICAgZ2VuX21hc2tzKHAgKyAxLCB4LCBjKTsKfQoKaW50IG1haW4oKSB7CiAgICBpb3M6OnN5bmNfd2l0aF9zdGRpbygwKTsgY2luLnRpZSgwKTsKICAgIHNpZXZlKCk7CiAgICBjaW4gPj4gbjsKICAgIGZvKGksIG4pIHsKICAgICAgICBjaW4gPj4gYVtpXTsKICAgIH0KICAgIGNpbiA+PiBxOwogICAgZm8oaXQsIHEpIHsKICAgICAgICBjaW4gPj4gbFtpdF0gPj4gcltpdF0gPj4gWFtpdF07CiAgICAgICAgZVtsW2l0XS0xXS5wYigtaXQpOwogICAgICAgIGVbcltpdF1dLnBiKGl0KTsKICAgIH0KICAgIGZvKGksIG4pIHsKICAgICAgICBnZW5fZGl2aXNvcnMoYVtpXSk7CiAgICAgICAgZm9yKGludCBqIDogZGl2aSkgY250W2pdKys7CiAgICAgICAgZm9yKGludCB0ID0gMDsgdCA8IGVbaV0uc2l6ZSgpOyArK3QpIHsKICAgICAgICAgICAgaW50IGlkID0gZVtpXVt0XSwgeCA9IFhbYWJzKGlkKV07CiAgICAgICAgICAgIHByLmNsZWFyKCk7CiAgICAgICAgICAgIHdoaWxlKHggPiAxKSB7CiAgICAgICAgICAgICAgICBpbnQgeSA9IGxwW3hdOwogICAgICAgICAgICAgICAgcHIucGIoeSk7CiAgICAgICAgICAgICAgICB3aGlsZSh4ICUgeSA9PSAwKSB4IC89IHk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgc2lnbiA9IChpZCA8IDAgPyAtMSA6IDEpLCBjdXJfaWQgPSBhYnMoaWQpOwogICAgICAgICAgICBnZW5fbWFza3MoKTsKICAgICAgICB9CiAgICB9CiAgICBmbyhpdCwgcSkgY291dCA8PCBhbnNbaXRdIDw8ICdcbic7CgogICAgcmV0dXJuIDA7Cn0=