#include <bits/stdc++.h>
using namespace std;
const long long md = 1e9 + 7;
const int lim = 1e7 + 1;
long long mexp(long long a, long long e) {
  long long res = 1;
  while (e) {
    if (e & 1) res = res * a % md;
    a = a * a % md;
    e /= 2;
  }
  return res;
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  vector<int> gpf(lim);
  for (int p = (2); p < (lim); ++p)
    if (!gpf[p])
      for (int i = p; i < lim; i += p) gpf[i] = p;
  int n;
  cin >> n;
  vector<vector<int>> gr(n);
  for (int _ = (1); _ < (n); ++_) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    gr[u].push_back(v);
    gr[v].push_back(u);
  }
  vector<int> a(n);
  for (auto &x : a) cin >> x;
  int lg = 0;
  while ((1 << lg) < n) ++lg;
  vector<vector<int>> hopp(lg, vector<int>(n, -1));
  vector<int> h(n);
  vector<int> lnum(n), rnum(n);
  int ix = 0;
  function<void(int)> dfs = [&](int v) {
    lnum[v] = ix++;
    for (auto &u : gr[v])
      if (u != hopp[0][v]) {
        hopp[0][u] = v;
        h[u] = h[v] + 1;
        dfs(u);
      }
    rnum[v] = ix++;
  };
  dfs(0);
  hopp[0][0] = 0;
  for (int i = (1); i < (lg); ++i)
    for (int j = (0); j < (n); ++j) hopp[i][j] = hopp[i - 1][hopp[i - 1][j]];
  auto jump = [&](int v, int l) {
    for (int i = 0; l; ++i) {
      if (l & (1 << i)) {
        v = hopp[i][v];
        l -= 1 << i;
      }
    }
    return v;
  };
  auto lca = [&](int u, int v) {
    if (h[u] > h[v]) swap(u, v);
    v = jump(v, h[v] - h[u]);
    if (u == v) return u;
    for (int i = (lg); i-- > (0);) {
      int vv = hopp[i][v], uu = hopp[i][u];
      if (vv != uu) {
        v = vv;
        u = uu;
      }
    }
    return hopp[0][u];
  };
  auto factor = [&](int m, vector<int> &res) {
    while (m > 1) {
      int p = gpf[m], q = 1;
      while (m % p == 0) {
        m /= p;
        q *= p;
        res.push_back(q);
      }
    }
  };
  vector<vector<pair<int, int>>> pre(lim);
  for (int i = (0); i < (n); ++i) {
    vector<int> ls;
    factor(a[i], ls);
    for (auto &q : ls) {
      pre[q].emplace_back(lnum[i], 1);
      pre[q].emplace_back(rnum[i], -1);
    }
  }
  for (auto &m : pre) {
    if (m.empty()) continue;
    sort((m).begin(), (m).end());
    for (int i = (1); i < (int(m.size())); ++i) m[i].second += m[i - 1].second;
  }
  auto cnt = [&](int q, int pos) {
    int lo = -1, hi = int(pre[q].size());
    while (lo + 1 < hi) {
      int mi = (lo + hi) / 2;
      if (pre[q][mi].first > pos)
        hi = mi;
      else
        lo = mi;
    }
    if (lo == -1) return 0;
    return pre[q][lo].second;
  };
  int q;
  cin >> q;
  for (int _ = (0); _ < (q); ++_) {
    int u, v, x;
    cin >> u >> v >> x;
    --u, --v;
    int w = lca(u, v);
    vector<int> ls;
    factor(x, ls);
    long long ans = 1;
    for (auto &q : ls) {
      if (pre[q].empty()) continue;
      int c = cnt(q, lnum[u]) + cnt(q, lnum[v]) - cnt(q, lnum[w]) -
              cnt(q, lnum[w] - 1);
      ans *= mexp(gpf[q], c);
      ans %= md;
    }
    cout << ans << endl;
  }
}
