#include <bits/stdc++.h>
using namespace std;
int n;
int q;
vector<int> v[100007];
long long ans[100007];
int LCA[100007][18];
int lvl[100007];
int st[100007];
int en[100007];
int a[100007];
int vec_sz[10000007];
struct query {
  int x, y, z;
  int lca_val;
};
query p[100007];
bool pr[100007];
vector<pair<int, int> > srt[10000007];
void init() {
  for (int i = 2; i <= 3400; ++i) {
    if (pr[i] == true) {
      continue;
    }
    for (int j = 2 * i; j <= n; j += i) {
      pr[j] = false;
    }
    for (int j = 1; j <= n; ++j) {
      int cnt = 0;
      while ((a[j] % i) == 0) {
        ++cnt;
        a[j] /= i;
      }
      if (cnt > 0) {
        srt[i].push_back({cnt, j});
      }
    }
    for (int j = 1; j <= q; ++j) {
      int cnt = 0;
      while ((p[j].z % i) == 0) {
        ++cnt;
        p[j].z /= i;
      }
      if (cnt > 0) {
        srt[i].push_back({cnt, n + j});
      }
    }
    vec_sz[i] = srt[i].size();
    sort(srt[i].begin(), srt[i].end());
  }
  for (int j = 1; j <= n; ++j) {
    if (a[j] > 1) {
      srt[a[j]].push_back({1, j});
      ++vec_sz[a[j]];
    }
  }
  for (int j = 1; j <= q; ++j) {
    if (p[j].z > 1) {
      srt[p[j].z].push_back({1, n + j});
      ++vec_sz[p[j].z];
    }
  }
}
int time_var;
void dfs(int vertex, int prv) {
  st[vertex] = ++time_var;
  for (int i = 1; i < 18; ++i) {
    LCA[vertex][i] = LCA[LCA[vertex][i - 1]][i - 1];
  }
  int sz = v[vertex].size();
  for (int i = 0; i < sz; ++i) {
    if (v[vertex][i] == prv) {
      continue;
    }
    lvl[v[vertex][i]] = lvl[vertex] + 1;
    LCA[v[vertex][i]][0] = vertex;
    dfs(v[vertex][i], vertex);
  }
  en[vertex] = time_var;
}
int get_LCA(int x, int y) {
  for (int i = 18 - 1; i >= 0; --i) {
    if (lvl[x] - (1 << i) >= lvl[y]) {
      x = LCA[x][i];
    }
    if (lvl[y] - (1 << i) >= lvl[x]) {
      y = LCA[y][i];
    }
  }
  for (int i = 18 - 1; i >= 0; --i) {
    if (LCA[x][i] != LCA[y][i]) {
      x = LCA[x][i];
      y = LCA[y][i];
    }
  }
  if (x != y) {
    x = LCA[x][0];
  }
  return x;
}
class Tree {
 public:
  int tr[5 * 100007];
  int cnt[5 * 100007];
  void init(int where, int IL, int IR) {
    tr[where] = 0;
    if (IL == IR) {
      return;
    }
    int mid = (IL + IR) / 2;
    init(2 * where, IL, mid);
    init(2 * where + 1, mid + 1, IR);
  }
  void update(int where, int IL, int IR, int CURL, int CURR, int val) {
    if (CURL > CURR) {
      return;
    }
    if (IR < CURL || CURR < IL) {
      return;
    }
    if (CURL <= IL && IR <= CURR) {
      tr[where] = (tr[where] + val);
      while (tr[where] >= 1000000007) {
        tr[where] -= 1000000007;
      }
      while (tr[where] < 0) {
        tr[where] += 1000000007;
      }
      if (val > 0) {
        ++cnt[where];
      } else {
        --cnt[where];
      }
      return;
    }
    int mid = (IL + IR) / 2;
    if (CURL <= mid) {
      update(2 * where, IL, mid, CURL, CURR, val);
    }
    if (mid + 1 <= CURR) {
      update(2 * where + 1, mid + 1, IR, CURL, CURR, val);
    }
  }
  int query(int where, int IL, int IR, int pos) {
    if (IR < pos || pos < IL) {
      return 0;
    }
    if (IL == IR) {
      return tr[where];
    }
    int mid = (IL + IR) / 2;
    int ret = 0;
    if (pos <= mid) {
      ret = query(2 * where, IL, mid, pos);
    } else {
      ret = query(2 * where + 1, mid + 1, IR, pos);
    }
    ret += tr[where];
    while (ret >= 1000000007) {
      ret -= 1000000007;
    }
    return ret;
  }
  int get_cnt(int where, int IL, int IR, int pos) {
    if (IR < pos || pos < IL) {
      return 0;
    }
    if (IL == IR) {
      return cnt[where];
    }
    int mid = (IL + IR) / 2;
    int ret = 0;
    if (pos <= mid) {
      ret = get_cnt(2 * where, IL, mid, pos);
    } else {
      ret = get_cnt(2 * where + 1, mid + 1, IR, pos);
    }
    ret += cnt[where];
    return ret;
  }
};
Tree w;
long long fastpow(long long x, long long pw) {
  long long ret = 1;
  while (pw > 0) {
    if ((pw & 1) == 0) {
      x = (x * x) % 1000000007;
      pw /= 2;
    } else {
      ret = (ret * x) % 1000000007;
      --pw;
    }
  }
  return ret;
}
void input() {
  scanf("%d", &n);
  for (int i = 1, x, y; i < n; ++i) {
    scanf("%d%d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; ++i) {
    scanf("%d%d%d", &p[i].x, &p[i].y, &p[i].z);
  }
}
void solve() {
  init();
  dfs(1, -1);
  for (int i = 1; i <= q; ++i) {
    ans[i] = 1;
    p[i].lca_val = get_LCA(p[i].x, p[i].y);
  }
  w.init(1, 1, n);
  for (int i = 2; i < 10000007; ++i) {
    if (vec_sz[i] == 0) {
      continue;
    }
    int sz = vec_sz[i];
    for (int j = 0; j < sz; ++j) {
      int ver = srt[i][j].second;
      if (ver <= n) {
        int add = srt[i][j].first;
        w.update(1, 1, n, st[ver], en[ver], add);
      } else {
        ver -= n;
        long long ret1 = w.query(1, 1, n, st[p[ver].x]);
        long long ret2 = w.query(1, 1, n, st[p[ver].y]);
        long long ret3 = w.query(1, 1, n, st[p[ver].lca_val]);
        long long ret4 = w.query(1, 1, n, st[LCA[p[ver].lca_val][0]]);
        ret1 =
            (ret1 + ret2 - ret3 - ret4 + 1000000007 + 1000000007) % 1000000007;
        long long aux = fastpow(i, ret1);
        ans[ver] = (ans[ver] * aux) % 1000000007;
      }
    }
    for (int j = 0; j < sz; ++j) {
      int ver = srt[i][j].second;
      if (ver <= n) {
        int add = srt[i][j].first;
        w.update(1, 1, n, st[ver], en[ver], -add);
      } else {
        ver -= n;
        long long ret1 = w.get_cnt(1, 1, n, st[p[ver].x]);
        long long ret2 = w.get_cnt(1, 1, n, st[p[ver].y]);
        long long ret3 = w.get_cnt(1, 1, n, st[p[ver].lca_val]);
        long long ret4 = w.get_cnt(1, 1, n, st[LCA[p[ver].lca_val][0]]);
        ret1 =
            (ret1 + ret2 - ret3 - ret4 + 1000000007 + 1000000007) % 1000000007;
        ret1 = (ret1 * srt[i][j].first) % 1000000007;
        long long aux = fastpow(i, ret1);
        ans[ver] = (ans[ver] * aux) % 1000000007;
      }
    }
  }
  for (int i = 1; i <= q; ++i) {
    printf("%I64d\n", ans[i]);
  }
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  input();
  solve();
  return 0;
}
