#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 14, maxv = 1e7 + 1, lg = 17, lgv = 25, mod = 1e9 + 7;
int n, q, a[maxn], fp[maxv], par[lg][maxn], h[maxn];
bool pr[maxv];
vector<int> g[maxn];
int *num[maxv];
struct AQ {
  int id, cuf;
};
struct PrimeData {
  int p, have, sum;
};
vector<PrimeData> prs[maxn];
vector<AQ> assign[maxn];
void dfs(int v = 0) {
  for (auto u : g[v])
    if (u != par[0][v]) {
      h[u] = h[v] + 1;
      par[0][u] = v;
      dfs(u);
    }
}
int lca(int v, int u) {
  if (h[u] < h[v]) swap(v, u);
  for (int i = 0; i < lg; i++)
    if (h[u] - h[v] >> i & 1) u = par[i][u];
  for (int i = lg - 1; i >= 0; i--)
    if (par[i][v] != par[i][u]) {
      v = par[i][v];
      u = par[i][u];
    }
  return v == u ? v : par[0][v];
}
int po(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1, a = (long long)a * a % mod)
    if (b & 1) ans = (long long)ans * a % mod;
  return ans;
}
void add(int x, int v) {
  while (x != 1) {
    int p = fp[x], c = 0;
    while (x % p == 0) {
      c++;
      x /= p;
    }
    num[p][c] += v;
  }
}
void solve(int v = 0) {
  add(a[v], 1);
  for (auto [i, c] : assign[v])
    for (auto &[p, h, s] : prs[i])
      for (int i = 1; i < lgv; i++) s += c * min(h, i) * num[p][i];
  for (auto u : g[v])
    if (u != par[0][v]) solve(u);
  add(a[v], -1);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  for (int i = 2; i < maxv; i++)
    if (!pr[i]) {
      num[i] = new int[lgv];
      fill(num[i], num[i] + lgv, 0);
      fp[i] = i;
      for (long long j = (long long)i * i; j < maxv; j += i) {
        pr[j] = 1;
        fp[j] = i;
      }
    }
  cin >> n;
  for (int i = 1; i < n; i++) {
    int v, u;
    cin >> v >> u;
    v--, u--;
    g[v].push_back(u);
    g[u].push_back(v);
  }
  for (int i = 0; i < n; i++) cin >> a[i];
  dfs();
  for (int i = 1; i < lg; i++)
    for (int v = 0; v < n; v++) par[i][v] = par[i - 1][par[i - 1][v]];
  cin >> q;
  for (int i = 0; i < q; i++) {
    int v, u, x;
    cin >> v >> u >> x;
    v--, u--;
    int p = lca(v, u);
    assign[v].push_back({i, 1});
    assign[u].push_back({i, 1});
    assign[p].push_back({i, -1});
    if (p) assign[par[0][p]].push_back({i, -1});
    while (x != 1) {
      int p = fp[x], c = 0;
      while (x % p == 0) {
        c++;
        x /= p;
      }
      prs[i].push_back({p, c, 0});
    }
  }
  solve();
  for (int i = 0; i < q; i++) {
    int ans = 1;
    for (auto [p, h, s] : prs[i]) ans = (long long)ans * po(p, s) % mod;
    cout << ans << '\n';
  }
}
