#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
// 計算模逆元的函數
ll modInverse(ll a, ll mod) {
ll m0 = mod, y = 0, x = 1;
if (mod == 1) return 0;
while (a > 1) {
ll q = a / mod;
ll t = mod;
mod = a % mod, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0) x += m0;
return x;
}
int main(){
int t;
cin >> t;
while(t--){
ll n, q, ans = 1;
cin >> n >> q;
vector<int> a(n), b(n), c(n), d(n);
for(int i = 0; i < n; i++){
cin >> a[i];
c[i] = a[i];
}
for(int i = 0; i < n; i++){
cin >> b[i];
d[i] = b[i];
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
for(int i = 0; i < n; i++){
ans = (ans * min(a[i], b[i])) % MOD;
}
cout << ans << " ";
while(q--){
int o, x;
cin >> o >> x;
x--;
if(o == 1){
auto it = upper_bound(a.begin(), a.end(), c[x]) - 1 - a.begin();
c[x]++;
ll index = min(a[it], b[it]);
a[it]++;
ans = (ans * min(a[it], b[it])) % MOD;
ans = (ans * modInverse(index, MOD)) % MOD;
}
if(o == 2){
auto it = upper_bound(b.begin(), b.end(), d[x]) - 1 - b.begin();
d[x]++;
ll index = min(a[it], b[it]);
b[it]++;
ans = (ans * min(a[it], b[it])) % MOD;
ans = (ans * modInverse(index, MOD)) % MOD;
}
cout << ans % MOD << " ";
}
cout << endl;
}
}
I2luY2x1ZGU8dmVjdG9yPgojaW5jbHVkZTxpb3N0cmVhbT4KI2luY2x1ZGU8YWxnb3JpdGhtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKY29uc3QgbGwgTU9EID0gOTk4MjQ0MzUzOwoKLy8g6KiI566X5qih6YCG5YWD55qE5Ye95pW4CmxsIG1vZEludmVyc2UobGwgYSwgbGwgbW9kKSB7CiAgICBsbCBtMCA9IG1vZCwgeSA9IDAsIHggPSAxOwogICAgaWYgKG1vZCA9PSAxKSByZXR1cm4gMDsKICAgIHdoaWxlIChhID4gMSkgewogICAgICAgIGxsIHEgPSBhIC8gbW9kOwogICAgICAgIGxsIHQgPSBtb2Q7CiAgICAgICAgbW9kID0gYSAlIG1vZCwgYSA9IHQ7CiAgICAgICAgdCA9IHk7CiAgICAgICAgeSA9IHggLSBxICogeTsKICAgICAgICB4ID0gdDsKICAgIH0KICAgIGlmICh4IDwgMCkgeCArPSBtMDsKICAgIHJldHVybiB4Owp9CgppbnQgbWFpbigpewogICAgaW50IHQ7CiAgICBjaW4gPj4gdDsKICAgIHdoaWxlKHQtLSl7CiAgICAgICAgbGwgbiwgcSwgYW5zID0gMTsKICAgICAgICBjaW4gPj4gbiA+PiBxOwogICAgICAgIHZlY3RvcjxpbnQ+IGEobiksIGIobiksIGMobiksIGQobik7CiAgICAgICAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKyl7CiAgICAgICAgICAgIGNpbiA+PiBhW2ldOwogICAgICAgICAgICBjW2ldID0gYVtpXTsKICAgICAgICB9CiAgICAgICAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKyl7CiAgICAgICAgICAgIGNpbiA+PiBiW2ldOwogICAgICAgICAgICBkW2ldID0gYltpXTsKICAgICAgICB9CiAgICAgICAgc29ydChhLmJlZ2luKCksIGEuZW5kKCkpOwogICAgICAgIHNvcnQoYi5iZWdpbigpLCBiLmVuZCgpKTsKICAgICAgICBmb3IoaW50IGkgPSAwOyBpIDwgbjsgaSsrKXsKICAgICAgICAgICAgYW5zID0gIChhbnMgKiBtaW4oYVtpXSwgYltpXSkpICUgTU9EOwogICAgICAgIH0KICAgICAgICBjb3V0IDw8IGFucyA8PCAiICI7CiAgICAgICAgd2hpbGUocS0tKXsKICAgICAgICAgICAgaW50IG8sIHg7CiAgICAgICAgICAgIGNpbiA+PiBvID4+IHg7CiAgICAgICAgICAgIHgtLTsKICAgICAgICAgICAgaWYobyA9PSAxKXsKICAgICAgICAgICAgICAgIGF1dG8gaXQgPSB1cHBlcl9ib3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AoyIDQKMSA3CjEgMQoyIDEyCjEgNQo1IDMKMTAwMDAwMDAgMjAwMDAwMDAgMzAwMDAwMDAgNDAwMDAwMDAgNTAwMDAwMDAKMTAwMDAwMDAgMjAwMDAwMDAgMzAwMDAwMDAgNDAwMDAwMDAgNTAwMDAwMDAKMSAxCjIgMgoyIDEK
4
3 4
1 1 2
3 2 1
1 3
2 3
1 1
2 1
6 8
1 4 2 7 3 5
7 6 5 6 3 3
2 5
1 6
1 5
1 5
1 5
2 3
2 3
1 6
13 8
7 7 6 6 5 5 5 2 2 3 4 5 1
1 4 1 9 6 6 9 1 5 1 3 8 4
2 2
2 11
2 4
2 4
1 7
1 1
2 12
1 5
5 3
10000000 20000000 30000000 40000000 50000000
10000000 20000000 30000000 40000000 50000000
1 1
2 2
2 1