#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
const int M = 1000000007;
int n, v[N];
vector<vector<int> > g;
vector<vector<pair<int, int> > > f;
bool notPrime[10001] = {1, 1};
int p[N];
int d[N];
int sz[N];
void DFS(int u) {
  sz[u] = 1;
  for (auto &v : g[u]) {
    if (v != p[u]) {
      p[v] = u;
      d[v] = d[u] + 1;
      DFS(v);
      sz[u] += sz[v];
    }
  }
}
int c, ch[N], chl[N], chr[N], chH[N];
int eb[N], ee[N];
vector<pair<int, int> > val;
void build(int u) {
  if (ch[u] == -1) {
    ch[u] = c;
    chH[c] = u;
    chl[c] = val.size();
    chr[c] = chl[c] - 1;
    ++c;
  }
  eb[u] = val.size();
  for (auto &x : f[u]) {
    val.push_back(x);
    ++chr[ch[u]];
  }
  ee[u] = val.size() - 1;
  int mx = -1, at = -1;
  for (auto &v : g[u])
    if (v != p[u]) {
      if (mx < sz[v]) {
        mx = sz[v];
        at = v;
      }
    }
  if (mx != -1) {
    ch[at] = ch[u];
    build(at);
  }
  for (auto &v : g[u])
    if (v != at && v != p[u]) build(v);
}
vector<pair<int, int> > seg[8 * 4 * N];
vector<int> cs[8 * 4 * N];
void build(int n, int s, int e) {
  if (s == e) {
    seg[n].push_back(val[s]);
    cs[n].push_back(val[s].second);
    return;
  }
  build(n * 2, s, (s + e) / 2);
  build(n * 2 + 1, (s + e) / 2 + 1, e);
  auto &a = seg[n * 2];
  auto &b = seg[n * 2 + 1];
  auto &c = seg[n];
  int i = 0, j = 0;
  while (i < a.size() && j < b.size()) {
    if (a[i] <= b[j])
      c.push_back(a[i++]);
    else
      c.push_back(b[j++]);
  }
  while (i < a.size()) c.push_back(a[i++]);
  while (j < b.size()) c.push_back(b[j++]);
  int total = 0;
  for (int i = 0; i < seg[n].size(); ++i) {
    total += seg[n][i].second;
    cs[n].push_back(total);
  }
}
vector<pair<int, int> > cur;
vector<pair<int, int> > ans;
int ql, qr;
int w;
void get(int n, int s, int e) {
  if (qr < s || ql > e) return;
  if (s >= ql && e <= qr) {
    for (int i = 0; i < cur.size(); ++i) {
      auto &a = cur[i];
      int l = lower_bound(seg[n].begin(), seg[n].end(), make_pair(a.first, 0)) -
              seg[n].begin();
      int m = lower_bound(seg[n].begin(), seg[n].end(),
                          make_pair(a.first, a.second + 1)) -
              seg[n].begin();
      --m;
      int r =
          lower_bound(seg[n].begin(), seg[n].end(), make_pair(a.first + 1, 0)) -
          seg[n].begin();
      --r;
      if (l <= r) {
        if (m >= 0) ans[i].second += cs[n][m];
        if (l > 0) ans[i].second -= cs[n][l - 1];
        ans[i].second += a.second * (r - m);
      }
    }
    return;
  }
  get(n * 2, s, (s + e) / 2);
  get(n * 2 + 1, (s + e) / 2 + 1, e);
}
int Pow(int b, int p) {
  if (!p) return 1;
  long long z = Pow(b, p / 2);
  if (p & 1) return z * z % M * b % M;
  return z * z % M;
}
int calc(int u, int v) {
  if (cur.empty()) return 1;
  ans = cur;
  for (auto &x : ans) x.second = 0;
  while (true) {
    int ch1 = chH[ch[u]];
    int ch2 = chH[ch[v]];
    if (ch1 == ch2) break;
    if (d[ch1] < d[ch2]) swap(u, v);
    ql = eb[chH[ch[u]]];
    qr = ee[u];
    get(1, 1, w);
    u = p[chH[ch[u]]];
  }
  if (d[u] > d[v]) swap(u, v);
  ql = eb[u];
  qr = ee[v];
  get(1, 1, w);
  int res = 1;
  for (auto &x : ans) res = (long long)res * Pow(x.first, x.second) % M;
  return res;
}
int main() {
  memset(ch, -1, sizeof(ch));
  for (int i = 2; i * i <= 10000; ++i)
    if (!notPrime[i])
      for (int j = i * i; j <= 10000; j += i) notPrime[j] = true;
  vector<int> P;
  for (int i = 2; i <= 10000; ++i)
    if (!notPrime[i]) P.push_back(i);
  scanf("%d", &n);
  g.resize(n);
  for (int i = 1, a, b; i < n; ++i) {
    scanf("%d%d", &a, &b);
    --a;
    --b;
    g[a].push_back(b);
    swap(a, b);
    g[a].push_back(b);
  }
  f.resize(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &v[i]);
    int x = v[i];
    for (int j = 0; j < P.size() && P[j] * P[j] <= x; ++j) {
      if (x % P[j] == 0) {
        f[i].push_back({P[j], 0});
        while (x % P[j] == 0) {
          x /= P[j];
          ++f[i].back().second;
        }
      }
    }
    if (x != 1) f[i].push_back({x, 1});
  }
  p[0] = -1;
  val.push_back({0, 0});
  DFS(0);
  build(0);
  w = val.size() - 1;
  build(1, 1, w);
  int q;
  scanf("%d", &q);
  while (q--) {
    int u, v, a;
    scanf("%d%d%d", &u, &v, &a);
    --u;
    --v;
    cur.clear();
    int x = a;
    for (int j = 0; j < P.size() && P[j] * P[j] <= x; ++j) {
      if (x % P[j] == 0) {
        cur.push_back({P[j], 0});
        while (x % P[j] == 0) {
          x /= P[j];
          ++cur.back().second;
        }
      }
    }
    if (x != 1) cur.push_back({x, 1});
    printf("%d\n", calc(u, v));
  }
  return 0;
}
