#include <bits/stdc++.h>
inline long long rd() {
  long long _x = 0;
  int _ch = getchar(), _f = 1;
  for (; !isdigit(_ch) && (_ch != '-') && (_ch != EOF); _ch = getchar())
    ;
  if (_ch == '-') {
    _f = 0;
    _ch = getchar();
  }
  for (; isdigit(_ch); _ch = getchar()) _x = _x * 10 + _ch - '0';
  return _f ? _x : -_x;
}
void write(long long _x) {
  if (_x >= 10)
    write(_x / 10), putchar(_x % 10 + '0');
  else
    putchar(_x + '0');
}
inline void wrt(long long _x, char _p) {
  if (_x < 0) putchar('-'), _x = -_x;
  write(_x);
  if (_p) putchar(_p);
}
int n, q, a[100005];
int ans[100005];
inline int qpow(int x, int k) {
  int ans = 1;
  for (; k; k >>= 1, x = 1ll * x * x % 1000000007)
    if (k & 1) ans = 1ll * ans * x % 1000000007;
  return ans;
}
std::vector<std::pair<int, std::pair<int, int>>> query[100005];
int Min[10000005], pri[10000005 / 10], tp;
bool vis[10000005];
void euler(int mx) {
  for (int i = 2; i <= mx; i++) {
    if (!vis[i]) pri[++tp] = i, Min[i] = tp;
    for (int j = 1; j <= tp && i * pri[j] <= mx; j++) {
      Min[i * pri[j]] = j;
      vis[i * pri[j]] = 1;
      if (i % pri[j] == 0) break;
    }
  }
}
int cnt[10000005 / 10][31];
int f[100005][20], dep[100005];
std::vector<int> G[100005];
void dfs(int u, int fa) {
  f[u][0] = fa;
  dep[u] = dep[fa] + 1;
  for (int i = 1; i <= 19; i++) f[u][i] = f[f[u][i - 1]][i - 1];
  for (auto i : G[u])
    if (i != fa) {
      dfs(i, u);
    }
}
inline int lca(int u, int v) {
  if (dep[u] < dep[v]) std::swap(u, v);
  for (int i = 19; i >= 0; i--)
    if (dep[f[u][i]] >= dep[v]) {
      u = f[u][i];
    }
  if (u == v) return u;
  for (int i = 19; i >= 0; i--)
    if (f[u][i] != f[v][i]) {
      u = f[u][i];
      v = f[v][i];
    }
  return f[u][0];
}
std::vector<std::pair<int, int>> div(int x) {
  std::vector<std::pair<int, int>> ans;
  while (x > 1) {
    int t = Min[x], c = 0;
    while (x % pri[t] == 0) x /= pri[t], c++;
    ans.push_back(std::make_pair(t, c));
  }
  return ans;
}
void solve(int u, int fa) {
  std::vector<std::pair<int, int>> now = div(a[u]);
  for (auto i : now) {
    for (int j = 1; j <= i.second; j++) {
      cnt[i.first][j]++;
    }
  }
  for (auto i : query[u]) {
    int w = i.first;
    std::vector<std::pair<int, int>> t = div(w);
    int R = 1;
    for (auto p : t) {
      int C = 0;
      for (int j = 1; j <= p.second; j++) {
        R = 1ll * R * qpow(pri[p.first], cnt[p.first][j]) % 1000000007;
        C += cnt[p.first][j];
      }
    }
    if (i.second.second == -1) {
      ans[i.second.first] =
          1ll * ans[i.second.first] * qpow(R, 1000000007 - 2) % 1000000007;
    } else {
      ans[i.second.first] = 1ll * ans[i.second.first] * R % 1000000007;
    }
  }
  for (auto i : G[u])
    if (i != fa) {
      solve(i, u);
    }
  for (auto i : now) {
    for (int j = 1; j <= i.second; j++) {
      cnt[i.first][j]--;
    }
  }
}
int main() {
  n = rd();
  for (int i = 2; i <= n; i++) {
    int x = rd(), y = rd();
    G[x].push_back(y), G[y].push_back(x);
  }
  int MAX = 0;
  for (int i = 1; i <= n; i++) {
    a[i] = rd();
    MAX = std::max(MAX, a[i]);
  }
  euler(10000000);
  dfs(1, 0);
  q = rd();
  for (int i = 1; i <= q; i++) {
    int u = rd(), v = rd(), w = rd();
    int l = lca(u, v);
    ans[i] = 1;
    query[u].push_back(std::make_pair(w, std::make_pair(i, 1)));
    query[v].push_back(std::make_pair(w, std::make_pair(i, 1)));
    query[l].push_back(std::make_pair(w, std::make_pair(i, -1)));
    if (l != 1)
      query[f[l][0]].push_back(std::make_pair(w, std::make_pair(i, -1)));
  }
  solve(1, 0);
  for (int i = 1; i <= q; i++) {
    wrt(ans[i], '\n');
  }
}
