#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, const U &b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, const U &b) {
  if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
  char c, sg = 0;
  while (c = getchar(), (c > '9' || c < '0') && c != '-')
    ;
  for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
       c = getchar())
    first = (first << 1) + (first << 3) + c - '0';
  if (sg) first = -first;
}
template <class T1, class T2>
inline void gn(T1 &x1, T2 &x2) {
  gn(x1), gn(x2);
}
int power(int a, int b, int m, int ans = 1) {
  for (; b; b >>= 1, a = 1LL * a * a % m)
    if (b & 1) ans = 1LL * ans * a % m;
  return ans;
}
int flag[10000010], id[10000010];
int prime[10000010 / 10], np;
void pre() {
  for (int i = 2; i < 10000010; i++) {
    if (!flag[i]) {
      flag[i] = i;
      id[i] = np;
      prime[np++] = i;
    }
    for (int j = 0; j < np && prime[j] <= flag[i] && prime[j] * i < 10000010;
         j++)
      flag[i * prime[j]] = prime[j];
  }
}
const int mod = 1000000007;
vector<int> adj[100100];
int L[100100], R[100100], rid;
int p[100100][20], dep[100100];
void dfs(int u, int f = 0) {
  L[u] = ++rid;
  dep[u] = dep[f] + 1;
  p[u][0] = f;
  for (int i = 1; i < 20; i++) p[u][i] = p[p[u][i - 1]][i - 1];
  for (int v : adj[u]) {
    if (v == f) continue;
    dfs(v, u);
  }
  R[u] = rid;
}
int lca(int u, int v) {
  if (dep[u] < dep[v]) swap(u, v);
  while (dep[u] > dep[v]) u = p[u][__builtin_ctz(dep[u] - dep[v])];
  if (u == v) return u;
  for (int i = 20; i--;) {
    if (p[u][i] != p[v][i]) u = p[u][i], v = p[v][i];
  }
  return p[u][0];
}
int ans[100100];
vector<pair<int, int> > num[10000010 / 10];
vector<pair<int, int> > que[10000010 / 10];
pair<int, int> operator+(pair<int, int> a, pair<int, int> b) {
  return pair<int, int>(a.first + b.first, a.second + b.second);
}
pair<int, int> operator-(pair<int, int> a, pair<int, int> b) {
  return pair<int, int>(a.first - b.first, a.second - b.second);
}
int B[100100];
void update(int u, int v) {
  for (; u <= rid; u += u & -u) B[u] += v;
}
int calc(int u, int ans = 0) {
  for (; u; u -= u & -u) ans += B[u];
  return ans;
}
int U[100100], V[100100], W[100100];
int main() {
  pre();
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    gn(u, v);
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1);
  for (int i = 1; i <= n; i++) {
    int u;
    gn(u);
    while (u > 1) {
      int v = flag[u], k = 0;
      while (u % v == 0) k++, u /= v;
      num[id[v]].push_back(pair<int, int>(k, i));
    }
  }
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int u;
    gn(U[i], V[i]), gn(u);
    W[i] = lca(U[i], V[i]);
    while (u > 1) {
      int v = flag[u], k = 0;
      while (u % v == 0) k++, u /= v;
      que[id[v]].push_back(pair<int, int>(k, i));
    }
    ans[i] = 1;
  }
  for (int i = 0; i < np; i++)
    if (!que[i].empty() && !num[i].empty()) {
      sort(que[i].begin(), que[i].end());
      for (int j = 0, k = 0; j < que[i].size(); j = k) {
        while (k < que[i].size() && que[i][j].first == que[i][k].first) k++;
        int d = que[i][j].first;
        for (pair<int, int> p : num[i]) {
          update(L[p.second], min(d, p.first));
          update(R[p.second] + 1, -min(d, p.first));
        }
        for (; j < k; j++) {
          int id = que[i][j].second;
          int ct = calc(L[U[id]]) + calc(L[V[id]]) - calc(L[W[id]]) -
                   calc(L[p[W[id]][0]]);
          ans[id] = (long long)ans[id] * power(prime[i], ct, mod) % mod;
        }
        for (pair<int, int> p : num[i]) {
          update(L[p.second], -min(d, p.first));
          update(R[p.second] + 1, min(d, p.first));
        }
      }
    }
  for (int i = 0; i < m; i++) printf("%d\n", ans[i]);
  return 0;
}
