#include <bits/stdc++.h>
using namespace std;
#pragma optimize("-O3")
int n, l;
const int N = 1e5 + 5;
const int M = 1e7 + 5;
const long long int MOD = 1e9 + 7;
long long int ans[N];
vector<pair<int, int>> qu;
vector<vector<int>> adj;
vector<vector<pair<pair<int, int>, int>>> pr;
int a[N];
int sv[M];
int t[4 * N + 5];
int timer;
vector<int> tin, tout;
vector<vector<int>> up;
int ind[N];
map<pair<int, int>, int> ma;
void dfs(int v, int p) {
  timer++;
  tin[v] = timer;
  ind[v] = timer;
  up[v][0] = p;
  for (int i = 1; i <= l; ++i) up[v][i] = up[up[v][i - 1]][i - 1];
  for (int u : adj[v]) {
    if (u != p) dfs(u, v);
  }
  tout[v] = timer;
}
bool is_ancestor(int u, int v) {
  return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int lca(int u, int v) {
  if (is_ancestor(u, v)) return u;
  if (is_ancestor(v, u)) return v;
  for (int i = l; i >= 0; --i) {
    if (!is_ancestor(up[u][i], v)) u = up[u][i];
  }
  return up[u][0];
}
void preprocess(int root) {
  tin.resize(n + 1);
  tout.resize(n + 1);
  timer = 0;
  l = ceil(log2(n + 1));
  up.assign(n + 1, vector<int>(l + 1));
  dfs(root, root);
}
int sum(int v, int tl, int tr, int l, int r) {
  if (l > r) return 0;
  if (l == tl && r == tr) {
    return t[v];
  }
  int tm = (tl + tr) / 2;
  return sum(v * 2, tl, tm, l, min(r, tm)) +
         sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
}
void update(int v, int tl, int tr, int pos, int new_val) {
  if (tl == tr) {
    t[v] += new_val;
  } else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      update(v * 2, tl, tm, pos, new_val);
    else
      update(v * 2 + 1, tm + 1, tr, pos, new_val);
    t[v] = t[v * 2] + t[v * 2 + 1];
  }
}
long long int add(int x, int y) { return (x + y) % MOD; }
long long int sub(int x, int y) { return add(x, MOD - y); }
long long int mul(int x, int y) { return (x * 1ll * y) % MOD; }
long long int binpow(int x, int y) {
  long long int z = 1;
  while (y > 0) {
    if (y % 2 == 1) z = mul(z, x);
    x = mul(x, x);
    y /= 2;
  }
  return z;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  adj.resize(n + 1);
  pr.resize(M);
  for (int i = 2; i <= 10000000; i++) {
    if (sv[i] != 0) {
      continue;
    }
    for (int j = 1; j * i <= 10000000; j++) {
      if (sv[i * j] == 0) {
        sv[i * j] = i;
      }
    }
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  preprocess(1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    int val = a[i];
    vector<pair<int, int>> vec;
    while (val != 1) {
      int v2 = sv[val];
      val = val / v2;
      if (vec.empty()) {
        vec.push_back({v2, 1});
      } else if (vec.back().first == v2) {
        int num = vec.back().second;
        vec.pop_back();
        vec.push_back({v2, num + 1});
      } else {
        vec.push_back({v2, 1});
      }
    }
    for (auto u : vec) {
      pr[u.first].push_back({{u.second, 0}, i});
      ma[{u.first, i}] = u.second;
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    ans[i] = 1;
  }
  for (int i = 0; i < q; i++) {
    int u, v, x;
    cin >> u >> v >> x;
    qu.push_back({u, v});
    int val = x;
    vector<pair<int, int>> vec;
    while (val != 1) {
      int v2 = sv[val];
      val = val / v2;
      if (vec.empty()) {
        vec.push_back({v2, 1});
      } else if (vec.back().first == v2) {
        int num = vec.back().second;
        vec.pop_back();
        vec.push_back({v2, num + 1});
      } else {
        vec.push_back({v2, 1});
      }
    }
    for (auto u : vec) {
      pr[u.first].push_back({{u.second, 1}, i});
    }
  }
  for (int i = 0; i < M; i++) {
    sort(pr[i].begin(), pr[i].end());
  }
  for (int i = 1; i <= M; i++) {
    if (pr[i].size() == 0) {
      continue;
    }
    vector<int> upd;
    int curr = 0;
    for (int j = 0; j < pr[i].size(); j++) {
      int inc;
      if (pr[i][j].first.first > curr) {
        for (int k = j; k < pr[i].size(); k++) {
          inc = pr[i][j].first.first - curr;
          if (pr[i][k].first.second == 0) {
            update(1, 0, N, tin[pr[i][k].second], inc);
            update(1, 0, N, tout[pr[i][k].second] + 1, -inc);
            upd.push_back(tin[pr[i][k].second]);
            upd.push_back(tout[pr[i][k].second] + 1);
          }
        }
        curr = pr[i][j].first.first;
      }
      if (pr[i][j].first.second == 1) {
        int quer = pr[i][j].second;
        int u = qu[quer].first;
        int v = qu[quer].second;
        int lc = lca(u, v);
        int val = sum(1, 0, N, 0, tin[u]) + sum(1, 0, N, 0, tin[v]) -
                  2 * sum(1, 0, N, 0, tin[lc]) +
                  min(ma[{i, lc}], pr[i][j].first.first);
        ans[quer] = mul(ans[quer], binpow(i, val));
      }
    }
    for (auto u : upd) {
      int val2 = sum(1, 0, N, u, u);
      update(1, 0, N, u, -val2);
    }
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << '\n';
  }
  return 0;
}
