#include <bits/stdc++.h>
using namespace std;
char cb[1 << 16], *cs = cb, *ct = cb;
void read(int &res) {
  char ch;
  for (; !isdigit(
           ch = (cs == ct && (ct = (cs = cb) + fread(cb, 1, 1 << 16, stdin),
                             cs == ct)
                     ? 0
                     : *cs++));)
    ;
  for (res = ch - '0';
       isdigit(ch = (cs == ct && (ct = (cs = cb) + fread(cb, 1, 1 << 16, stdin),
                                 cs == ct)
                         ? 0
                         : *cs++));
       res = res * 10 + ch - '0')
    ;
}
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
int Pow(int b, int k) {
  int r = 1;
  for (; k; k >>= 1, b = 1ll * b * b % 1000000007)
    if (k & 1) r = 1ll * r * b % 1000000007;
  return r;
}
int pr[10000007], vis[10000007], cnt_pr, mpr[10000007], tmr[10000007],
    inv[10000007], a[100005], ans[100005], iva[100005];
int n;
int info[100005], Prev[100005 << 1], to[100005 << 1], cnt_e;
void Node(int u, int v) {
  Prev[++cnt_e] = info[u], info[u] = cnt_e, to[cnt_e] = v;
}
int dep[100005], fa[100005], son[100005], sz[100005], tp[100005], sx[100005],
    ty[100005], cw[100005];
void dfs0(int u, int ff) {
  dep[u] = dep[fa[u] = ff] + 1;
  sz[u] = 1;
  for (int i = info[u], v; i; i = Prev[i])
    if ((v = to[i]) ^ ff)
      dfs0(v, u), sz[u] += sz[v], (sz[son[u]] < sz[v]) && (son[u] = v);
}
void dfs1(int u, int ff) {
  if (son[u]) tp[son[u]] = tp[u], dfs1(son[u], u);
  for (int i = info[u], v; i; i = Prev[i])
    if ((v = to[i]) ^ ff && v ^ son[u]) dfs1(tp[v] = v, u);
}
int Lca(int u, int v) {
  for (; tp[u] ^ tp[v]; u = fa[tp[u]])
    if (dep[tp[u]] < dep[tp[v]]) swap(u, v);
  return dep[u] < dep[v] ? u : v;
}
vector<pair<int, int> > G[100005];
int sm[10000007];
void dfs(int u, int ff) {
  for (int v = a[u], t; v > 1;) {
    t = mpr[v];
    for (int pw = t; mpr[v] == t; pw *= t) {
      v = tmr[v];
      sm[pw] = 1ll * sm[pw] * t % 1000000007;
    }
  }
  for (int i = (0), LIM = (G[u].size() - 1); i <= LIM; i++) {
    int v = G[u][i].first, tp = G[u][i].second;
    for (int t = cw[v], p; t > 1;) {
      p = mpr[t];
      for (int pw = p; mpr[t] == p; pw *= p) {
        t = tmr[t];
        if (tp == 1)
          ans[v] = 1ll * ans[v] * sm[pw] % 1000000007;
        else
          iva[v] = 1ll * iva[v] * sm[pw] % 1000000007;
      }
    }
  }
  for (int i = info[u], v; i; i = Prev[i])
    if ((v = to[i]) ^ ff) dfs(v, u);
  for (int v = a[u], t; v > 1;) {
    t = mpr[v];
    for (int pw = t; mpr[v] == t; pw *= t) {
      v = tmr[v];
      sm[pw] = 1ll * sm[pw] * inv[t] % 1000000007;
    }
  }
}
int main() {
  inv[1] = sm[1] = 1;
  for (int i = 2; i < 10000007; i++) {
    sm[i] = 1;
    if (!vis[i]) pr[cnt_pr++] = i, mpr[i] = i, inv[i] = Pow(i, 1000000007 - 2);
    for (int j = 0;; j++) {
      int t = i * pr[j];
      if (t >= 10000007) break;
      vis[t] = 1;
      inv[t] = 1ll * inv[i] * inv[pr[j]] % 1000000007;
      mpr[t] = pr[j];
      tmr[t] = i;
      if (i % pr[j] == 0) break;
    }
  }
  read(n);
  for (int i = (1), LIM = (n - 1); i <= LIM; i++) {
    int u, v;
    read(u), read(v);
    Node(u, v), Node(v, u);
  }
  dfs0(1, 0);
  dfs1(tp[1] = 1, 0);
  for (int i = (1), LIM = (n); i <= LIM; i++) read(a[i]);
  int q;
  read(q);
  for (int i = (1), LIM = (q); i <= LIM; i++) {
    read(sx[i]), read(ty[i]), read(cw[i]);
    int t = Lca(sx[i], ty[i]);
    G[sx[i]].push_back(make_pair(i, 1)), G[ty[i]].push_back(make_pair(i, 1)),
        G[t].push_back(make_pair(i, -1));
    ans[i] = gcd(a[t], cw[i]), iva[i] = 1;
  }
  dfs(1, 0);
  for (int i = (1), LIM = (q); i <= LIM; i++)
    printf("%d\n", 1ll * ans[i] * Pow(iva[i], 1000000007 - 3) % 1000000007);
}
