#include <cstdio>
int r, y, g;
int wait(long long t){
int sumtime = r + y+ g;
t = t%=sumtime;
if(t < r){
return r - t;
}else if(t < r+g){
return 0;
}else{
return sumtime - t + r;
}
}
int main(){
scanf("%d %d %d", &r, &y, &g);
int n;
scanf("%d", &n);
long long sum = 0;
for(int i=0; i<n; i++){
int k, t;
scanf("%d %d", &k, &t);
if(k == 0){
sum += t;
}else if(k == 1){
long long temp = sum + r - t;
int time = wait(temp);
sum += time;
}else if(k == 2){
long long temp = sum + r + g + y - t;
int time = wait(temp);
sum += time;
}else if(k == 3){
long long temp = sum + r + g - t;
int time = wait(temp);
sum += time;
}
}
printf("%lld", sum);
return 0;
}