#include <bits/stdc++.h>
using std::vector;
inline int read() {
  int out(0), c(getchar());
  for (; c < '0' || c > '9'; c = getchar())
    ;
  for (; c <= '9' && c >= '0'; c = getchar())
    out = (out << 3) + (out << 1) + (c ^ 48);
  return out;
}
const int MAXN = 1e5 + 100, MAXR = 1e7 + 100, R = 1e7;
const int pm = 1e9 + 7;
int N, Q;
int a[MAXN];
int ans[MAXN];
int pri[664579 + 100], pn;
vector<int> to[MAXN];
int vec[664579 + 100][25];
int mi[18][MAXN << 1], _log[MAXN << 1];
int fs[MAXN], dep[MAXN], cnt;
struct QR {
  int i, w;
  bool inv;
};
vector<QR> q[MAXN];
void pre() {
  static bool inp[MAXR];
  for (int i = 2; i <= R; ++i) {
    if (!inp[i]) pri[++pn] = i;
    for (int j = 1; j <= pn && i * pri[j] <= R; ++j) {
      inp[i * pri[j]] = 1;
      if (i % pri[j] == 0) break;
    }
  }
}
int cal(const int a, const int b) { return dep[a] < dep[b] ? a : b; }
void dfs0(const int x, const int fa) {
  fs[x] = ++cnt;
  mi[0][cnt] = x;
  for (int tt : to[x]) {
    if (tt != fa) {
      dep[tt] = dep[x] + 1;
      dfs0(tt, x);
      mi[0][++cnt] = x;
    }
  }
}
int gt_lca(int x, int y) {
  x = fs[x], y = fs[y];
  if (x > y) std::swap(x, y);
  const int k = _log[y - x + 1];
  return cal(mi[k][x], mi[k][y - (1 << k) + 1]);
}
int gcd(const int a, const int b) { return b ? gcd(b, a % b) : a; }
int qpow(int a, int b) {
  int out = 1;
  for (; b; b >>= 1, a = (long long)a * a % pm)
    if (b & 1) out = (long long)out * a % pm;
  return out;
}
void dfs(const int x, const int fa) {
  int v = a[x];
  for (int i = 1; i <= pn && pri[i] * pri[i] <= v; ++i) {
    if (v % pri[i] == 0) {
      int cnt = 0;
      while (v % pri[i] == 0) v /= pri[i], ++cnt;
      ++vec[i][cnt];
    }
  }
  if (v != 1) ++vec[std::lower_bound(pri + 1, pri + pn + 1, v) - pri][1];
  for (QR qr : q[x]) {
    v = qr.w;
    int tmp = 1;
    for (int i = 1; i <= pn && pri[i] * pri[i] <= v; ++i) {
      if (v % pri[i] == 0) {
        int cnt = 0;
        while (v % pri[i] == 0) v /= pri[i], ++cnt;
        int p = pri[i];
        for (int j = 1; j < cnt; ++j, p = (long long)p * pri[i] % pm)
          tmp = (long long)tmp * qpow(p, vec[i][j]) % pm;
        for (int j = cnt; j <= 24; ++j)
          tmp = (long long)tmp * qpow(p, vec[i][j]) % pm;
      }
    }
    if (v != 1) {
      const int pi = std::lower_bound(pri + 1, pri + pn + 1, v) - pri;
      for (int j = 1; j <= 24; ++j)
        tmp = (long long)tmp * qpow(v, vec[pi][j]) % pm;
    }
    ans[qr.i] = (long long)ans[qr.i] *
                (qr.inv ? qpow((long long)tmp * tmp % pm, pm - 2) : tmp) % pm;
  }
  v = a[x];
  for (int tt : to[x])
    if (tt != fa) dfs(tt, x);
  for (int i = 1; i <= pn && pri[i] * pri[i] <= v; ++i) {
    if (v % pri[i] == 0) {
      int cnt = 0;
      while (v % pri[i] == 0) v /= pri[i], ++cnt;
      --vec[i][cnt];
    }
  }
  if (v != 1) --vec[std::lower_bound(pri + 1, pri + pn + 1, v) - pri][1];
}
int main() {
  pre();
  N = read();
  for (int i = 1, x, y; i < N; ++i) {
    x = read(), y = read();
    to[x].push_back(y);
    to[y].push_back(x);
  }
  for (int i = 1; i <= N; ++i) a[i] = read();
  dfs0(1, 0);
  for (int i = 1; i <= 17; ++i)
    for (int s = (1 << i); s < (1 << (i + 1)) && s <= cnt; ++s) _log[s] = i;
  for (int i = 1; i <= 17; ++i)
    for (int j = 1; j <= cnt; ++j)
      mi[i][j] = cal(
          mi[i - 1][j],
          mi[i - 1]
            [((cnt) < (j + (1 << (i - 1))) ? (cnt) : (j + (1 << (i - 1))))]);
  Q = read();
  for (int i = 1, x, y, w; i <= Q; ++i) {
    x = read(), y = read(), w = read();
    const int p = gt_lca(x, y);
    ans[i] = gcd(w, a[p]);
    q[x].push_back({i, w, 0});
    q[y].push_back({i, w, 0});
    q[p].push_back({i, w, 1});
  }
  dfs(1, 0);
  for (int i = 1; i <= Q; ++i) printf("%d\n", ans[i]);
  return 0;
}
