Problem: Let the main characters of this problem be personages from some recent movie. New Avengers seem to make a lot of buzz. I didn't watch any part of the franchise and don't know its heroes well, but it won't stop me from using them in this problem statement. So, Thanos and Dr. Strange are doing their superhero and supervillain stuff, but then suddenly they stumble across a regular competitive programming problem.

You are given a tree with n vertices.

In each vertex v there is positive integer a_{v}.

You have to answer q queries.

Each query has a from u v x.

You have to calculate ∏_{w ∈ P} gcd(x, a_{w}) mod (10^{9} + 7), where P is a set of vertices on path from u to v. In other words, you are to calculate the product of gcd(x, a_{w}) for all vertices w on the path from u to v. As it might be large, compute it modulo 10^9+7. Here gcd(s, t) denotes the greatest common divisor of s and t.

Note that the numbers in vertices do not change after queries.

I suppose that you are more interested in superhero business of Thanos and Dr. Strange than in them solving the problem. So you are invited to solve this problem instead of them.

Input

In the first line of input there is one integer n (1 ≤ n ≤ 10^{5}) — the size of the tree.

In the next n-1 lines the edges of the tree are described. The i-th edge is described with two integers u_{i} and v_{i} (1 ≤ u_{i}, v_{i} ≤ n) and it connects the vertices u_{i} and v_{i}. It is guaranteed that graph with these edges is a tree.

In the next line there are n integers a_1, a_2, …, a_n (1 ≤ a_{v} ≤ 10^{7}).

In the next line there is one integer q (1 ≤ q ≤ 10^{5}) — the number of queries.

And in the next q lines the queries are described. Each query is described with three integers u_{i}, v_{i} and x_{i} (1 ≤ u_{i}, v_{i} ≤ n, 1 ≤ x_{i} ≤ 10^{7}).

Output

Print q numbers — the answers to the queries in the order they are given in the input. Print each answer modulo 10^9+7 = 1000000007. Print each number on a separate line.

Examples

Input

4
1 2
1 3
1 4
6 4 9 5
3
2 3 6
2 3 2
3 4 7


Output

36
4
1


Input

6
1 2
2 3
2 4
1 5
5 6
100000 200000 500000 40000 800000 250000
3
3 5 10000000
6 2 3500000
4 1 64000


Output

196000
12250
999998215

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int N = 1e5 + 10;
const int M = 1e7 + 10;
const int A = 18;
struct node {
  int num;
  vector<pair<int, int> > item;
  friend bool operator<(const node &a, const node &b) { return a.num < b.num; }
} pri[N << 3];
struct query {
  int num, x, id;
  friend bool operator<(const query &a, const query &b) {
    return a.num < b.num;
  }
};
struct Ask {
  int x, y, z;
} ask[N];
vector<query> q[N << 3];
int et;
int k = 0, cnt = 0;
int n, m, ti;
int l[N], r[N], deep[N], c[N], f[N], have[M];
int p[N][A], ans[N * 32];
long long ret;
vector<int> v[N];
void dfs(int x, int fa, int dep) {
  l[x] = ++ti;
  deep[x] = dep;
  if (fa) {
    p[x][0] = fa;
    for (int i = 0; p[p[x][i]][i]; ++i) p[x][i + 1] = p[p[x][i]][i];
  }
  for (auto u : v[x]) {
    if (u == fa) continue;
    dfs(u, x, dep + 1);
  }
  r[x] = ti;
}
int LCA(int a, int b) {
  if (deep[a] < deep[b]) swap(a, b);
  for (int i = 0, delta = deep[a] - deep[b]; delta; delta >>= 1, ++i)
    if (delta & 1) a = p[a][i];
  if (a == b) return a;
  for (int i(17); i >= (0); --i)
    if (p[a][i] != p[b][i]) {
      a = p[a][i];
      b = p[b][i];
    }
  return p[a][0];
}
vector<pair<int, int> > split(int x) {
  vector<pair<int, int> > ret;
  for (int i = 2; i * i <= x; ++i) {
    if (x % i == 0) {
      int cnt = 0;
      while (x % i == 0) x /= i, ++cnt;
      ret.push_back(make_pair(i, cnt));
    }
  }
  if (x > 1) ret.push_back(make_pair(x, 1));
  return ret;
}
inline void add(int x, int val) {
  for (; x <= n; x += x & -x) f[x] += val;
}
inline void update(int x, int val) {
  for (; x <= n; x += x & -x) c[x] += val;
}
inline int query_c(int x) {
  int ret = 0;
  for (; x; x -= x & -x) ret += c[x];
  return ret;
}
inline int query_f(int x) {
  int ret = 0;
  for (; x; x -= x & -x) ret += f[x];
  return ret;
}
inline long long Pow(long long a, long long b, long long mod) {
  long long ret(1);
  for (; b; b >>= 1, (a *= a) %= mod)
    if (b & 1) (ret *= a) %= mod;
  return ret;
}
inline void clr(int x) {
  for (; x <= n; x += x & -x) c[x] = f[x] = 0;
}
int main() {
  scanf("%d", &n);
  for (int i(2); i <= (n); ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }
  ti = 0;
  dfs(1, 0, 1);
  for (int i(1); i <= (n); ++i) {
    int num;
    scanf("%d", &num);
    vector<pair<int, int> > cc = split(num);
    for (auto u : cc) {
      int x = u.first, y = u.second;
      if (have[x]) {
        pri[have[x]].item.push_back(make_pair(y, i));
      } else {
        have[x] = ++cnt;
        pri[cnt].num = x;
        pri[cnt].item.push_back(make_pair(y, i));
      }
    }
  }
  for (int i(1); i <= (cnt); ++i) sort(pri[i].item.begin(), pri[i].item.end());
  sort(pri + 1, pri + cnt + 1);
  for (int i(1); i <= (cnt); ++i) have[pri[i].num] = i;
  scanf("%d", &m);
  for (int i(1); i <= (m); ++i) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    ask[i] = {x, y, z};
    int lca = LCA(x, y), plca = p[lca][0];
    vector<pair<int, int> > cc = split(z);
    for (auto u : cc) {
      if (!have[u.first]) continue;
      int id = have[u.first];
      q[id].push_back({u.second, x, ++k});
      q[id].push_back({u.second, y, ++k});
      q[id].push_back({u.second, lca, ++k});
      q[id].push_back({u.second, plca, ++k});
    }
  }
  for (int i(1); i <= (cnt); ++i) sort(q[i].begin(), q[i].end());
  for (int i(1); i <= (cnt); ++i) {
    set<int> mark;
    for (auto u : pri[i].item) {
      int x = u.second, y = u.first;
      add(l[x], 1);
      add(r[x] + 1, -1);
      mark.insert(x);
    }
    int sz = pri[i].item.size();
    int now = 0;
    for (auto u : q[i]) {
      while (now < sz && u.num >= pri[i].item[now].first) {
        int x = pri[i].item[now].second;
        int y = pri[i].item[now].first;
        update(l[x], y);
        update(r[x] + 1, -y);
        add(l[x], -1);
        add(r[x] + 1, 1);
        mark.insert(x);
        ++now;
      }
      ans[u.id] = query_c(l[u.x]) + query_f(l[u.x]) * u.num;
    }
    for (auto u : mark) {
      clr(l[u]);
      clr(r[u] + 1);
    }
  }
  et = 0;
  for (int i(1); i <= (m); ++i) {
    int x = ask[i].x, y = ask[i].y, z = ask[i].z;
    ret = 1;
    int lca = LCA(x, y), plca = p[lca][0];
    vector<pair<int, int> > cc = split(z);
    for (auto u : cc) {
      if (!have[u.first]) continue;
      int now = 0;
      ++et;
      now += ans[et];
      ++et;
      now += ans[et];
      ++et;
      now -= ans[et];
      ++et;
      now -= ans[et];
      ret = ret * Pow(u.first, now, mod) % mod;
    }
    printf("%lld\n", ret);
  }
  return 0;
}