#include<bits/stdc++.h>
using namespace std;
const int mod = 998244353; int t, n, cnt[8][900005], luythua[255][100005], chon[255], ctrc[255]; vector<int> uoc[900005];
void prepare () {
for (int i = 1; i < 900005; i++) for (int j = i; j < 900005; j += i) uoc[j].push_back(i);
luythua[0][0] = 1;
for (int i = 1; i < 255; i++) {
luythua[i][0] = 1;
for (int j = 1; j < 100005; j++) luythua[i][j] = 1LL*luythua[i][j - 1]*i % mod;
}
}
int getCount(int val, int maxx) {
const vector<int> &wish = uoc[val];
int hh = wish.size(), phu = 0;
memset(chon, 0, sizeof (int)*(hh + 1));
memset(ctrc, 0, sizeof (int)*(hh + 1));
for (int low = 1; low <= 7; low++) if (cnt[low][maxx] != 0) {
while (phu < hh && wish[phu] < low) phu++;
if (phu == hh || cnt[low][wish[phu] - 1] > 0) return 0;
for (int i = phu; i < hh - 1; i++) chon[i - phu + 1] += cnt[low][wish[i + 1] - 1] - cnt[low][wish[i] - 1];
ctrc[hh - phu] += cnt[low][maxx] - cnt[low][wish[hh - 1] - 1];
}
long long res = 1, tmp1 = 1, tmp2 = 1;
for (int i = 1; i <= hh; i++) res = res*luythua[i][chon[i]] % mod, tmp1 = tmp1*luythua[i][ctrc[i]] % mod, tmp2 = tmp2*luythua[i - 1][ctrc[i]] % mod;
return 1LL * res * (tmp1 - tmp2 + mod) % mod;
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
prepare();
cin >> t;
while (t--) {
memset(cnt, 0, sizeof cnt);
cin >> n;
int maxx = 0;
for (int i = 1; i <= n; i++) {
int l, r; cin >> l >> r;
cnt[l][r]++;
maxx = max(maxx, r);
}
for (int l = 1; l <= 7; l++) for (int i = 1; i <= maxx; i++) cnt[l][i] += cnt[l][i - 1];
int res = 0;
for (int val = 1; val <= maxx; val++) res = (res + getCount(val, maxx)) % mod;
cout << res << '\n';
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKY29uc3Qg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ykgY2hvbltpIC0gcGh1ICsgMV0gKz0gY250W2xvd11bd2lzaFtpICsgMV0gLSAxXSAtIGNudFtsb3ddW3dpc2hbaV0gLSAxXTsKICAgICAgICBjdHJjW2hoIC0gcGh1XSArPSBjbnRbbG93XVttYXh4XSAtIGNudFtsb3ddW3dpc2hbaGggLSAxXSAtIDFdOwogICAgfQogICAgbG9uZyBsb25nIHJlcyA9IDEsIHRtcDEgPSAxLCB0bXAyID0gMTsKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IGhoOyBpKyspIHJlcyA9IHJlcypsdXl0aHVhW2ldW2Nob25baV1dICUgbW9kLCB0bXAxID0gdG1wMSpsdXl0aHVhW2ldW2N0cmNbaV1dICUgbW9kLCB0bXAyID0gdG1wMipsdXl0aHVhW2kgLSAxXVtjdHJjW2ldXSAlIG1vZDsKICAgIHJldHVybiAxTEwgKiByZXMgKiAodG1wMSAtIHRtcDIgKyBtb2QpICUgbW9kOwp9CnNpZ25lZCBtYWluKCkgewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsgY2luLnRpZSgwKTsgY291dC50aWUoMCk7CiAgICBwcmVwYXJlKCk7CiAgICBjaW4gPj4gdDsKICAgIHdoaWxlICh0LS0pIHsKICAgICAgICBtZW1zZXQoY250LCAwLCBzaXplb2YgY250KTsKICAgICAgICBjaW4gPj4gbjsKICAgICAgICBpbnQgbWF4eCA9IDA7CiAgICAgICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKSB7CiAgICAgICAgICAgIGludCBsLCByOyBjaW4gPj4gbCA+PiByOwogICAgICAgICAgICBjbnRbbF1bcl0rKzsKICAgICAgICAgICAgbWF4eCA9IG1heChtYXh4LCByKTsKICAgICAgICB9CiAgICAgICAgZm9yIChpbnQgbCA9IDE7IGwgPD0gNzsgbCsrKSBmb3IgKGludCBpID0gMTsgaSA8PSBtYXh4OyBpKyspIGNudFtsXVtpXSArPSBjbnRbbF1baSAtIDFdOwogICAgICAgIGludCByZXMgPSAwOwogICAgICAgIGZvciAoaW50IHZhbCA9IDE7IHZhbCA8PSBtYXh4OyB2YWwrKykgcmVzID0gKHJlcyArIGdldENvdW50KHZhbCwgbWF4eCkpICUgbW9kOwogICAgICAgIGNvdXQgPDwgcmVzIDw8ICdcbic7CiAgICB9Cn0K