#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int a[n],b[n];
for(int i=0;i<n;i++){
cin >> a[i];
}
for(int i=0;i<n;i++){
cin >> b[i];
}
stack<pair<int,int>> st;
for(int i=0;i<n;i++){
int right = a[i];
int left = a[i] - b[i] + 1;
if(st.size() == 0){
st.push(make_pair(right,left));
}
else{
int topl,topr;
topl = st.top().second;
topr = st.top().first;
while(left <= topr && st.size() > 0){
st.pop();
left = min(left,topl);
if(st.size() > 0){
topl = st.top().second;
topr = st.top().first;
}
}
st.push(make_pair(right,left));
}
}
long long ans(0);
while(st.size() > 0){
int topl,topr;
topl = st.top().second;
topr = st.top().first;
int len = topr - topl + 1;
ans += 1LL * len * (len + 1)/2;
cout << topr << " " << topl << endl;
st.pop();
}
cout << ans << endl;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbWFpbigpIHsKCWludCB0OwoJY2luID4+IHQ7Cgl3aGlsZSh0LS0pewoJCWludCBuOwoJCWNpbiA+PiBuOwoJCWludCBhW25dLGJbbl07CgkJZm9yKGludCBpPTA7aTxuO2krKyl7CgkJCWNpbiA+PiBhW2ldOwoJCX0KCQlmb3IoaW50IGk9MDtpPG47aSsrKXsKCQkJY2luID4+IGJbaV07CgkJfQoJCXN0YWNrPHBhaXI8aW50LGludD4+IHN0OwoJCWZvcihpbnQgaT0wO2k8bjtpKyspewoJCQlpbnQgcmlnaHQgPSBhW2ldOwoJCQlpbnQgbGVmdCA9IGFbaV0gLSBiW2ldICsgMTsKCQkJaWYoc3Quc2l6ZSgpID09IDApewoJCQkJc3QucHVzaChtYWtlX3BhaXIocmlnaHQsbGVmdCkpOwoJCQl9CgkJCWVsc2V7CgkJCQlpbnQgdG9wbCx0b3ByOwoJCQkJdG9wbCA9IHN0LnRvcCgpLnNlY29uZDsKCQkJCXRvcHIgPSBzdC50b3AoKS5maXJzdD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