#include<iostream>
using namespace std;
int main()
{
long long int r,g,y,n,k,t,total = 0,alltime;
cin >> r >> y >>g;
alltime = r + y + g;
cin >> n;
while(n--){
int temp = 0;
cin >> k >> t;
if(k == 0)
total += t;
else if(k == 1){
if((total - t ) <= 0)
total += (t - total);
else{
temp = (total - t) % alltime;
if(temp >= g && temp <= g + y){
total += (y + g - temp + r);
}
else if(temp >= g + y){
total += (r + g + y - temp);
}
}
}
else if(k == 2){
if((total - t ) <= 0)
total += (t - total + r);
else{
temp = (total - t) % alltime;
if(temp <= r){
total += (r - temp);
}
else if(temp >= r + g && temp <= r + g + y){
total += (r + g + y - temp + r);
}
}
}
else if(k == 3){
if((total - t ) < 0)
continue;
else{
temp = (total - t) % alltime;
if(temp <= y)
total += (r + y - temp);
else if(temp <= y + r && temp >= y){
total += ( y + r - temp);
}
}
}
}
cout << total;
}