#include "bits/stdc++.h"
using namespace std;
#define int long long
#ifdef LOCAL
#define watch(x) cout << #x << ':' << x << ' ';
#else
#define watch(x) 0
#endif
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
void init() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
freopen("err.txt", "w", stderr);
#endif
}
vector<int> v[30010], val, sieve, ans;
vector<map<int, int>> hmm;
int n;
void precomp() {
sieve = vector<int>(1000010);
for (int i = 0; i <= 1000000; i++) {
sieve[i] = i;
}
sieve[0] = sieve[1] = 0;
for (int i = 2; i <= 1000000; i++) {
if (sieve[i] == i) {
for (int j = i * i; j <= 1000000; j += i) {
sieve[j] = min(sieve[j], i);
}
}
}
}
void dfs(int x, int p) {
int z = val[x], d = sieve[val[x]];
while (z > 1) {
int c = 0;
while (z % d == 0) {
c++;
z /= d;
}
hmm[x].insert({d, c});
d = sieve[z];
}
for (int y : v[x]) {
if (y != p) {
dfs(y, x);
if (sz(hmm[x]) < sz(hmm[y])) {
swap(hmm[x], hmm[y]);
}
for (auto r : hmm[y]) {
int a = r.ff;
int b = r.ss;
if (hmm[x].count(a)) {
hmm[x][a] += b;
}
else hmm[x][a] = b;
}
}
}
int c = 1;
for (auto w : hmm[x]) {
c = (c * (w.ss + 1)) % mod;
}
ans[x] = c;
}
int32_t main()
{
IOS;
init();
int t = 1;
precomp();
cin >> t;
while (t--)
{
cin >> n;
for (int i = 0; i <= n; i++) v[i].clear();
val.assign(n + 1, 0);
hmm.assign(n + 1, {});
ans.assign(n + 1, 0);
for (int i = 0; i < n; i++) cin >> val[i + 1];
int a, b;
for (int i = 0; i < n - 1; i++) {
cin >> a >> b;
v[a].pb(b);
v[b].pb(a);
}
dfs(1, -1);
for (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