// #pragma GCC optimize(1)
// #pragma GCC optimize(2)
// #pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
// #include <bits/extc++.h>
// using namespace __gnu_cxx;
// using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
#define mod ll(1e9+7)
int t,opt,d,a,b;
struct node{
int a,b;
};
#define fastin ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
bool operator<(const node &a,const node &b){
if(a.a!=b.a)return a.a<b.a;
return a.b<b.b;
}
map<node,int>l,c;
int main(){
fastin;
cin>>t;
while(t--){
cin>>opt>>d>>a>>b;
if(opt == 1){
if(d == 0)l[(node){a,b}]++;
if(d == 1)c[(node){a,b}]++;
}else{
if(d == 0)l[(node){a,b}]--;
if(d == 1)c[(node){a,b}]--;
}
if(l.size() == 0 || c.size() == 0){
cout<<-1<<endl;
continue;
}
int ans = INT_MAX;
// for(auto i:l)printf("[(%d,%d),%d]",i.first.a,i.first.b,i.second);cout<<endl;
// for(auto i:c)printf("[(%d,%d),%d]",i.first.a,i.first.b,i.second);cout<<endl;
for(auto i:l){
if(i.second <= 0)continue;
for(auto j:c){
if(j.second <= 0)continue;
int res = max(i.first.a+j.first.a,i.first.b+j.first.b);
if(ans > res) ans = res;
else break;
}
}
cout<<ans<<endl;
}
return 0;
}