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 JAVA):

Solution:
import java.io.*;
import java.util.*;

public class Test {

    static int readInt() {
        int ans = 0;
        boolean neg = false;
        try {
            boolean start = false;
            for (int c = 0; (c = System.in.read()) != -1; ) {
                if (c == '-') {
                    start = true;
                    neg = true;
                    continue;
                } else if (c >= '0' && c <= '9') {
                    start = true;
                    ans = ans * 10 + c - '0';
                } else if (start) break;
            }
        } catch (IOException e) {
        }
        return neg ? -ans : ans;
    }

    static long readLong() {
        long ans = 0;
        boolean neg = false;
        try {
            boolean start = false;
            for (int c = 0; (c = System.in.read()) != -1; ) {
                if (c == '-') {
                    start = true;
                    neg = true;
                    continue;
                } else if (c >= '0' && c <= '9') {
                    start = true;
                    ans = ans * 10 + c - '0';
                } else if (start) break;
            }
        } catch (IOException e) {
        }
        return neg ? -ans : ans;
    }

    static String readString() {
        StringBuilder b = new StringBuilder();
        try {
            for (int c = 0; (c = System.in.read()) != -1; ) {
                if (c < '0' || c > '9') {
                    break;
                } else {
                    b.append((char) (c));
                }
            }
        } catch (IOException e) {
        }
        return b.toString().trim();
    }

    static PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

    List<Integer>[] g;
    final long M = 1_000_000_007;
    final int E = 23;
    int n;
    int[] a;
    int[][][] af;
    int[] afcnt;
    int[] par;
    int[] level;
    int[][] lca;
    int[] ps = new int[10_000_001];
    int[] qu, qv, qx;
    int[][][] qf;
    int[] qfcnt;
    int[] qlca;
    Map<Integer, int[]>[] fsum;
    Set<Integer>[] needed;
    Map<Integer, int[]> csum;


    void dfs(int p, int u, int lvl) {
        par[u] = p;
        level[u] = lvl;
        for (int v : g[u])
            if (v != p) dfs(u, v, lvl + 1);
    }

    void buildLCA() {
        for (int i = 0; i < n; i++) lca[i][0] = par[i];
        for (int j = 1; (1 << j) <= n; j++) {
            for (int i = 0; i < n; i++) {
                int p = lca[i][j - 1];
                if (p == -1) continue;
                lca[i][j] = lca[p][j - 1];
            }
        }
    }

    int getLCA(int u, int v) {
        if (u == v) return u;
        if (level[u] < level[v]) {
            int t = u;
            u = v;
            v = t;
        }
        int b = Integer.numberOfTrailingZeros(Integer.highestOneBit(level[u])) + 1;
        for (; b >= 0; b--)
            if (level[u] - (1 << b) >= level[v]) u = lca[u][b];
        if (u == v) return u;
        b = Integer.numberOfTrailingZeros(Integer.highestOneBit(level[v])) + 1;
        for (; b >= 0; b--)
            if (lca[u][b] != lca[v][b]) {
                u = lca[u][b];
                v = lca[v][b];
            }
        return lca[u][0];
    }

    int fact(int x, int[][] f) {
        int cnt = 0;
        while (ps[x] != 0) {
            int p = ps[x];
            int c = 0;
            while (x % p == 0) {
                c++;
                x /= p;
            }
            f[cnt][0] = p;
            f[cnt][1] = c;
            cnt++;
        }
        if (x > 1) {
            f[cnt][0] = x;
            f[cnt][1] = 1;
            cnt++;
        }
        return cnt;
    }

    void go(int p, int u) {
        int[][] fa = af[u];
        for (int i = afcnt[u] - 1; i >= 0; i--) {
            int f = fa[i][0], e = fa[i][1];
            int[] cs = csum.computeIfAbsent(f, x -> new int[E]);
            cs[e-1]++;
        }
        for (int f : needed[u]) {
            int[] cs = csum.computeIfAbsent(f, x -> new int[E]);
            int[] fs = fsum[u].computeIfAbsent(f, x -> new int[E]);
            System.arraycopy(cs, 0, fs, 0, fs.length);
        }
        for (int v : g[u])
            if (v != p) go(u, v);
        for (int i = afcnt[u] - 1; i >= 0; i--) {
            int f = fa[i][0], e = fa[i][1];
            int[] cs = csum.get(f);
            cs[e-1]--;
        }
    }

    int gcd(int u, int v) {
        if (v == 0) return u;
        return gcd(v, u % v);
    }

    long powmod(int f, int e) {
        long ans = 1;
        int b = Integer.highestOneBit(e);
        for (; b > 0; b >>>= 1) {
            ans = (ans * ans) % M;
            if ((b & e) != 0) ans = (ans * f) % M;
        }
        return ans;
    }

    void start() {
        n = readInt();
        a = new int[n];
        af = new int[n][10][2];
        afcnt = new int[n];
        par = new int[n];
        Arrays.fill(par, -1);
        level = new int[n];
        csum = new HashMap<>();
        fsum = new Map[n];
        for (int i = 0; i < n; i++) fsum[i] = new HashMap<>();
        needed = new Set[n];
        for (int i = 0; i < n; i++) needed[i] = new HashSet<>();
        lca = new int[n][Integer.numberOfTrailingZeros(Integer.highestOneBit(n)) + 1];
        for (int i = 0; i < n; i++) Arrays.fill(lca[i], -1);
        g = new List[n];
        for (int i = 0; i < n; i++) g[i] = new ArrayList<>();
        for (int i = 0; i < n - 1; i++) {
            int u = readInt(), v = readInt();
            u--;
            v--;
            g[u].add(v);
            g[v].add(u);
        }
        dfs(-1, 0, 0);
        buildLCA();
        for (int i = 0; i < n; i++) a[i] = readInt();
        for (int i = 2; i < ps.length; i++)
            if (ps[i] == 0) {
                for (int j = i + i; j < ps.length; j += i) ps[j] = i;
            }
        int q = readInt();
        qu = new int[q];
        qv = new int[q];
        qx = new int[q];
        qf = new int[q][10][2];
        qfcnt = new int[q];
        qlca = new int[q];
        for (int i = 0; i < q; i++) {
            int u = readInt(), v = readInt(), x = readInt();
            u--;
            v--;
            qu[i] = u;
            qv[i] = v;
            qx[i] = x;
            qlca[i] = getLCA(u, v);
            int[][] fa = qf[i];
            qfcnt[i] = fact(x, fa);
            for (int j = qfcnt[i] - 1; j >= 0; j--) {
                int f = fa[j][0];
                needed[u].add(f);
                needed[v].add(f);
                needed[qlca[i]].add(f);
            }
        }
        for (int i = 0; i < n; i++) afcnt[i] = fact(a[i], af[i]);
        go(-1, 0);
        int[] df = new int[E];
        for (int i = 0; i < q; i++) {
            int u = qu[i], v = qv[i], x = qx[i], r = qlca[i];
            long ans = 1;
            for (int j = qfcnt[i] - 1; j >= 0; j--) {
                int f = qf[i][j][0], e = qf[i][j][1];
                int[] fu = fsum[u].getOrDefault(f, df), fr = fsum[r].getOrDefault(f, df),
                        fv = fsum[v].getOrDefault(f, df);
                for (int k = 0; k < E; k++) {
                    int c = fu[k] - fr[k] + fv[k] - fr[k];
                    ans = (ans * powmod(f, Math.min(k + 1, e) * c)) % M;
                }
            }
            ans = ans * gcd(a[r], x) % M;
            writer.println(ans);
        }
    }

    public static void main(String[] args) {
        Test te = new Test();
        te.start();
        writer.flush();
    }
}