#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 3e5 + 10;
const double EPS = 1e-5;
const long long p = 43;
const long long INF = 1e18 + 21;
const long long MOD = 1e9 + 7;
const double pi = acos(-1);
template <typename T>
T sqr(T x) {
  return x * x;
}
int n, pr[30][MAXN], tin[MAXN], tout[MAXN], t = 0, u[MAXN], v[MAXN], q;
long long a[MAXN], x[MAXN];
int resh[(int)1e7 + 10];
map<int, int> ans[MAXN];
vector<int> g[MAXN];
map<int, int> razlx[MAXN], razla[MAXN];
vector<int> was;
set<int> is_spec;
set<int> spec[MAXN];
map<int, map<int, int> > spec_calc;
map<int, int> now_spec;
set<int> need[MAXN];
int now[3200][30];
map<int, int> get_razl(int x) {
  int now = x;
  map<int, int> ans_;
  while (now > 1) {
    ans_[resh[now]]++;
    now /= resh[now];
  }
  return ans_;
}
void count_pr0(int v) {
  was[v] = 1;
  tin[v] = t;
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i];
    if (was[to] == 0) {
      pr[0][to] = v;
      count_pr0(to);
    }
  }
  tout[v] = ++t;
}
bool is_ancestor(int b, int a) {
  return tin[b] <= tin[a] && tout[a] <= tout[b];
}
int lca(int a, int b) {
  if (is_ancestor(a, b)) {
    return a;
  }
  if (is_ancestor(b, a)) {
    return b;
  }
  int x = a;
  for (int i = 29; i > -1; i--) {
    if (!is_ancestor(pr[i][x], b)) {
      x = pr[i][x];
    }
  }
  return pr[0][x];
}
void spec_dfs(int v) {
  was[v] = 1;
  if (is_spec.count(x[v])) {
    now_spec[x[v]]++;
  }
  for (set<int, int>::iterator it = spec[v].begin(); it != spec[v].end();
       it++) {
    spec_calc[*it][v] = now_spec[*it];
  }
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i];
    if (was[to] == 0) {
      spec_dfs(to);
    }
  }
}
long long my_pow(const long long &x, long long st, const long long &mod) {
  if (st == 0) {
    return 1;
  }
  if (st % 2 == 0) {
    return sqr(my_pow(x, st / 2, mod)) % mod;
  } else {
    return my_pow(x, st - 1, mod) * x % mod;
  }
}
void dfs(int v_) {
  was[v_] = 1;
  for (map<int, int>::iterator it = razla[v_].begin(); it != razla[v_].end();
       it++) {
    for (int i = 1; i < 30; i++) {
      now[it->first][i] += min(i, it->second);
    }
  }
  for (set<int>::iterator it_ = need[v_].begin(); it_ != need[v_].end();
       it_++) {
    int kek = *it_;
    if (v_ == v[kek]) {
      for (map<int, int>::iterator it = razlx[kek].begin();
           it != razlx[kek].end(); it++) {
        ans[kek][it->first] += now[it->first][it->second];
      }
    }
    if (v_ == u[kek]) {
      for (map<int, int>::iterator it = razlx[kek].begin();
           it != razlx[kek].end(); it++) {
        ans[kek][it->first] += now[it->first][it->second];
      }
    }
    if (v_ == lca(v[kek], u[kek])) {
      for (map<int, int>::iterator it = razlx[kek].begin();
           it != razlx[kek].end(); it++) {
        ans[kek][it->first] -= 2 * now[it->first][it->second];
      }
    }
  }
  for (int i = 0; i < g[v_].size(); i++) {
    int to = g[v_][i];
    if (was[to] == 0) {
      dfs(to);
    }
  }
  for (map<int, int>::iterator it = razla[v_].begin(); it != razla[v_].end();
       it++) {
    for (int i = 1; i < 30; i++) {
      now[it->first][i] -= min(i, it->second);
    }
  }
}
int main() {
  for (int i = 2; i < 1e7 + 1; i++) {
    if (resh[i] == 0) {
      for (int j = i; j < 1e7 + 1; j += i) {
        resh[j] = i;
      }
    }
  }
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> u[0] >> v[0];
    u[0]--;
    v[0]--;
    g[u[0]].push_back(v[0]);
    g[v[0]].push_back(u[0]);
  }
  was.clear();
  was.resize(n, 0);
  count_pr0(0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    razla[i] = get_razl(a[i]);
  }
  for (int i = 1; i < 30; i++) {
    for (int j = 0; j < n; j++) {
      pr[i][j] = pr[i - 1][pr[i - 1][j]];
    }
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> u[i] >> v[i] >> x[i];
    u[i]--;
    v[i]--;
    razlx[i] = get_razl(x[i]);
    if (razlx[i].size() == 1 && razlx[i].begin()->second == 1 && x[i] > 3162) {
      is_spec.insert(x[i]);
      spec[u[i]].insert(x[i]);
      spec[v[i]].insert(x[i]);
      spec[lca(u[i], v[i])].insert(x[i]);
    } else {
      need[u[i]].insert(i);
      need[v[i]].insert(i);
      need[lca(u[i], v[i])].insert(i);
    }
  }
  was.clear();
  was.resize(n, 0);
  spec_dfs(0);
  for (int i = 0; i < q; i++) {
    if (razlx[i].size() == 1 && razlx[i].begin()->second == 1 && x[i] > 3162) {
      int sum_st = spec_calc[x[i]][v[i]] + spec_calc[x[i]][u[i]] -
                   2 * spec_calc[x[i]][lca(u[i], v[i])] +
                   (a[lca(v[i], u[i])] == x[i]);
      ans[i][x[i]] = sum_st;
    }
  }
  was.clear();
  was.resize(n, 0);
  dfs(0);
  for (int i = 0; i < q; i++) {
    long long tmp = 1;
    for (map<int, int>::iterator it = ans[i].begin(); it != ans[i].end();
         it++) {
      tmp *= my_pow((long long)it->first, it->second, MOD);
      tmp %= MOD;
    }
    int v_ = lca(u[i], v[i]);
    for (map<int, int>::iterator it = razla[v_].begin(); it != razla[v_].end();
         it++) {
      tmp *= my_pow((long long)it->first, min(it->second, razlx[i][it->first]),
                    MOD);
      tmp %= MOD;
    }
    cout << tmp << "\n";
  }
  return 0;
}
