#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC optimize("no-stack-protector,fast-math")
using namespace std;
const int N = 3e5 + 100, OO = 1e9 + 7, K = 1e7 + 4, T = 22, M = 1e9 + 7,
          P = 6151, SQ = 1300, lg = 22;
int pr[N][lg + 5], F[K + 5], cnt[K + 5], h[N], ans[N], num[N], nxt = 0, a[N];
bool mark[N];
vector<int> v[N];
vector<pair<pair<int, int>, pair<int, int> > > qu[N];
void dfs(int x, int y) {
  pr[x][0] = y;
  for (int i = 1; i <= lg; i++) pr[x][i] = pr[pr[x][i - 1]][i - 1];
  h[x] = h[y] + 1;
  for (auto u : v[x])
    if (u != y) dfs(u, x);
}
int Lca(int x, int y) {
  if (h[x] < h[y]) swap(x, y);
  for (int i = lg; i >= 0; i--)
    if (h[pr[x][i]] >= h[y]) x = pr[x][i];
  if (x == y) return x;
  for (int i = lg; i >= 0; i--)
    if (pr[x][i] != pr[y][i]) x = pr[x][i], y = pr[y][i];
  return pr[x][0];
}
void get(int x) {
  nxt = 0;
  while (x > 1) num[nxt++] = F[x], x /= F[x];
  num[nxt] = 0;
}
int pw(int x, int y) {
  int ct = 1;
  y %= M;
  while (y) {
    if (y & 1) ct = 1ll * ct * x % M;
    x = 1ll * x * x % M;
    y >>= 1;
  }
  return ct;
}
inline int pwt(int tp, int x, int y) {
  int ct = pw(x, y);
  if (tp == 1)
    return ct;
  else
    return pw(ct, M - 2);
}
void dfs_get(int x, int y) {
  get(a[x]);
  for (int i = 0; i < nxt; i++) {
    int j = i, cer = num[i];
    while (num[j + 1] == num[j]) j++, cer *= num[i];
    cnt[cer]++;
    i = j;
  }
  for (auto u : qu[x]) {
    int tp = u.first.first, p = u.first.second, nw = u.second.first,
        id = u.second.second;
    int w = 1, h = 0;
    while (1ll * w * p < K) h++, w *= p;
    int sum = 0;
    for (int i = h; i > nw; i--) sum += cnt[w], w /= p;
    ans[id] = (1ll * ans[id] * pwt(tp, w, sum)) % M;
    for (int i = nw; i >= 0; i--)
      ans[id] = 1ll * ans[id] * pwt(tp, w, cnt[w]) % M, w /= p;
  }
  for (auto u : v[x])
    if (u != y) dfs_get(u, x);
  get(a[x]);
  for (int i = 0; i < nxt; i++) {
    int j = i, cer = num[i];
    while (num[j + 1] == num[j]) j++, cer *= num[i];
    cnt[cer]--;
    i = j;
  }
}
int32_t main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (long long i = 2; i < K; i++) {
    if (F[i]) continue;
    F[i] = i;
    for (long long j = i * i; j < K; j += i)
      if (!F[j]) F[j] = i;
  }
  dfs(1, 0);
  int q;
  cin >> q;
  for (int tp = 1; tp <= q; tp++) {
    int x, y, w;
    cin >> x >> y >> w;
    get(w);
    int z = Lca(x, y);
    ans[tp] = 1;
    for (int i = 0; i < nxt; i++) {
      int j = i;
      while (num[j + 1] == num[j]) j++;
      qu[x].push_back({{+1, num[i]}, {j - i + 1, tp}});
      qu[y].push_back({{+1, num[i]}, {j - i + 1, tp}});
      qu[z].push_back({{-1, num[i]}, {j - i + 1, tp}});
      qu[pr[z][0]].push_back({{-1, num[i]}, {j - i + 1, tp}});
      i = j;
    }
  }
  dfs_get(1, 0);
  for (int tp = 1; tp <= q; tp++) cout << ans[tp] << endl;
  return 0;
}
