#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5 + 10;
const long long mod = 1e9 + 7;
const long long base = 1e18;
vector<long long> adj[maxn];
long long dep[maxn];
long long anc[maxn][20];
long long a[maxn];
int p[100 * maxn];
long long u[maxn];
long long v[maxn];
long long x[maxn];
long long siz[maxn];
void dfs(long long u, long long par) {
  anc[u][0] = par;
  for (int i = 1; i <= 19; i++) {
    anc[u][i] = anc[anc[u][i - 1]][i - 1];
  }
  siz[u] = 1;
  for (auto to : adj[u]) {
    if (to == par) continue;
    dep[to] = dep[u] + 1;
    dfs(to, u);
    siz[u] += siz[to];
  }
}
long long lca(long long x, long long y) {
  if (dep[x] < dep[y]) swap(x, y);
  long long kc = dep[x] - dep[y];
  for (int i = 19; i >= 0; i--) {
    if (kc & (1ll << i)) {
      x = anc[x][i];
    }
  }
  if (x == y) return x;
  for (int i = 19; i >= 0; i--) {
    if (anc[x][i] != anc[y][i]) {
      x = anc[x][i];
      y = anc[y][i];
    }
  }
  return anc[x][0];
}
long long cntnw = 0;
long long nchain = 1;
long long chainhead[maxn];
long long chainid[maxn];
long long id[maxn];
void hld(long long u, long long par) {
  if (!chainhead[nchain]) chainhead[nchain] = u;
  cntnw++;
  chainid[u] = nchain;
  id[u] = cntnw;
  long long nxt = -1;
  for (auto to : adj[u]) {
    if (to == par) continue;
    if (nxt == -1 || siz[nxt] < siz[to]) {
      nxt = to;
    }
  }
  if (nxt != -1) {
    hld(nxt, u);
  }
  for (auto to : adj[u]) {
    if (to == par || to == nxt) continue;
    nchain++;
    hld(to, u);
  }
}
long long n;
long long fwt[2 * maxn];
vector<long long> vt;
void update(long long id, long long diff) {
  vt.push_back(id);
  for (int i = id; i <= n; i += (i & (-i))) {
    fwt[i] += diff;
  }
}
long long get(long long l, long long r) {
  long long ans = 0;
  for (int i = r; i; i -= (i & (-i))) {
    ans += fwt[i];
  }
  for (int i = l - 1; i; i -= (i & (-i))) {
    ans -= fwt[i];
  }
  return ans;
}
long long get1(long long u, long long a) {
  long long p = chainid[u];
  long long chk = chainid[a];
  long long ans = 0;
  while (1) {
    if (p == chk) {
      ans += get(id[a], id[u]);
      break;
    }
    ans += get(id[chainhead[p]], id[u]);
    u = anc[chainhead[p]][0];
    p = chainid[u];
  }
  return ans;
}
long long mu(long long a, long long n) {
  if (n == 0) return 1;
  if (n == 1) return a;
  long long t = mu(a, n / 2);
  if (n % 2 == 0) return (t * t) % mod;
  return ((t * t) % mod * a) % mod;
}
vector<pair<long long, long long> > gr[10 * maxn];
vector<pair<long long, long long> > gr1[10 * maxn];
long long ans[maxn];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  if (fopen("t.inp", "r")) {
    freopen("test.inp", "r", stdin);
    freopen("test.out", "w", stdout);
  }
  long long th = 1e7;
  vector<long long> vt2;
  for (int i = 2; i <= th; i++) {
    if (p[i]) continue;
    vt2.push_back(i);
    for (int j = i; j <= th; j += i) {
      p[j] = i;
    }
  }
  cin >> n;
  for (int i = 1; i <= n - 1; i++) {
    long long x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  dfs(1, 0);
  hld(1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    long long t = a[i];
    while (t != 1) {
      long long h = p[t];
      long long cnt = 0;
      while (t % h == 0) {
        cnt++;
        t /= h;
      }
      h = lower_bound(vt2.begin(), vt2.end(), h) - vt2.begin();
      gr1[h].push_back(make_pair(cnt, i));
    }
  }
  long long q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> u[i] >> v[i] >> x[i];
    ans[i] = 1;
    long long t = x[i];
    while (t != 1) {
      long long h = p[t];
      long long cnt = 0;
      while (t % h == 0) {
        cnt++;
        t /= h;
      }
      h = lower_bound(vt2.begin(), vt2.end(), h) - vt2.begin();
      gr[h].push_back(make_pair(cnt, i));
    }
  }
  for (int i = 0; i <= vt2.size() - 1; i++) {
    if (!gr[i].size()) continue;
    sort(gr[i].begin(), gr[i].end());
    long long j = 0;
    for (int bit = 1; bit < 30; bit++) {
      for (auto to : gr1[i]) {
        if (to.first < bit) continue;
        long long h = id[to.second];
        update(h, 1);
      }
      while (j < gr[i].size() && gr[i][j].first == bit) {
        long long id1 = gr[i][j].second;
        long long h = lca(u[id1], v[id1]);
        long long cnt = get1(u[id1], h) + get1(v[id1], h) - get(id[h], id[h]);
        ans[id1] = (ans[id1] * mu(vt2[i], cnt)) % mod;
        j++;
      }
    }
    auto vt1 = vt;
    for (auto p : vt1) {
      update(p, -1);
    }
    vt.clear();
  }
  for (int i = 1; i <= q; i++) {
    cout << ans[i] << "\n";
  }
}
