D. Potion Brewing Class
/********************
Author: Nanfeng1997
Contest: Codeforces - Codeforces Round #778 (Div. 1 + Div. 2, based on Technocup 2022 Final Round)
URL: https://codeforces.com/contest/1654/problem/D
When: 2022-03-20 19:36:44
Memory: 256MB
Time: 3000ms
********************/
#include <bits/stdc++.h>
#define _ 0
using namespace std;
using LL = long long;
template <typename T> void chkmax(T &x, T y) { x = max(x, y); }
template <typename T> void chkmin(T &x, T y) { x = min(x, y); }
const int MOD = 998244353;
inline int mod(int x) {return x >= MOD ? x - MOD : x;}
inline int ksm(int a, int b) {
int ret = 1; a = mod(a);
for(; b; b >>= 1, a = 1LL * a * a % MOD) if(b & 1) ret = 1LL * ret * a % MOD;
return ret;
}
template<int MOD>
struct modint {
int x;
modint() {x = 0; }
modint(int y) {x = y;}
inline modint inv() const { return modint{ksm(x, MOD - 2)}; }
explicit inline operator int() { return x; }
friend inline modint operator + (const modint &a, const modint& b) { return modint(mod(a.x + b.x)); }
friend inline modint operator - (const modint &a, const modint& b) { return modint(mod(a.x - b.x + MOD)); }
friend inline modint operator * (const modint &a, const modint& b) { return modint(1ll * a.x * b.x % MOD); }
friend inline modint operator / (const modint &a, const modint& b) { return modint(1ll * a.x * b.inv().x % MOD); }
friend inline modint operator - (const modint &a) { return modint(mod(MOD - a.x)); }
friend inline modint& operator += (modint &a, const modint& b) { return a = a + b; }
friend inline modint& operator -= (modint &a, const modint& b) { return a = a - b; }
friend inline modint& operator *= (modint &a, const modint& b) { return a = a * b; }
friend inline modint& operator /= (modint &a, const modint& b) { return a = a / b; }
inline int operator == (const modint &b) { return x == b.x; }
inline int operator != (const modint &b) { return x != b.x; }
inline int operator < (const modint &a) { return x < a.x; }
inline int operator <= (const modint &a) { return x <= a.x; }
inline int operator > (const modint &a) { return x > a.x; }
inline int operator >= (const modint &a) { return x >= a.x; }
};
typedef modint<MOD> mint;
inline mint ksm(mint a, int b) {
mint ret = 1;
for(; b; b >>= 1, a = a * a ) if(b & 1) ret = ret * a ;
return ret;
}
const int N = 2e5 + 10;
int d[N]; //d[i] 表示 i 的最小质因子是谁
vector<int> factor[N]; //factor[i] 里面存储的是 i 的所有质因子(例如 factor[8] = {2,2,2})
inline void init() { //预处理
for(int i = N - 1; i > 1; i -- ) {
for(int j = i; j < N; j += i) {
d[j] = i;
}
}
for(int i = 1; i < N; i ++ ) {
for(int j = i; j != 1; j /= d[j]) {
factor[i].push_back(d[j]);
}
}
}
void solve() {
int n; cin >> n;
vector<vector<array<int, 3>> >son(n);
set<int> s; //存储用到的质因子
for(int i = 1; i < n; i ++ ) {
int u, v, x, y; cin >> u >> v >> x >> y;
-- u, -- v;
son[u].push_back({v, x, y});
son[v].push_back({u, y, x});
//将用到的质因子存储到 set
for(int &p: factor[x]) s.insert(p);
for(int &p: factor[y]) s.insert(p);
}
vector<int> f(n + 1, 0), wf(n + 1, 0);
//f数组是用来每次枚举质因子是加还是减
//wf 数组是用来记录在树的遍历的时候的每个质因子的最大值
function<void(int, int)> dfs1 = [&] (int u, int fa) {
for(auto &[v, x, y] : son[u]) {
if(v == fa) continue;
for(int &p: factor[y]) f[p] --;
for(int &p: factor[x]) f[p] ++, chkmax(wf[p], f[p]);
dfs1(v, u);
for(int &p: factor[y]) f[p] ++;
for(int &p: factor[x]) f[p] --;
}
};
dfs1(0, -1); //进行树的遍历,求出 wf 数组
mint ans = 0;
function<void(int, int, mint)> dfs2 = [&] (int u, int fa, mint res) {
ans += res;
mint tem = res;//这里记录一下是因为每个分叉是互不影响的
for(auto &[v, x, y]: son[u]) {
if(v == fa) continue;
res = tem;
for(int &p: factor[y]) res *= p;
for(int &p: factor[x]) res /= p;
//求出每个节点应该有的值
dfs2(v, u, res);
}
};
mint res = 1;
//求出 1号节点应该有的值(即0号点)
for(int x: s) {
for(int i = 0; i < wf[x]; i ++ ) {
res *= x;
}
}
dfs2(0, -1, res); //dfs2 求出答案!
cout << (int)ans << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
init();
int T = 1; cin >> T;
while(T --) solve();
return ~~(0 ^ _ ^ 0);
}