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;
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;
}