#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int K = 1e4 + 5;
vector<long long int> edges;
long long int h, a[N], sp[K];
int n, m, k, c[N];
bool mark[N];
set<pair<int, int> > s;
set<pair<int, int> >::iterator it;
void add(long long int x) {
  memset(sp, -1, sizeof sp);
  edges.push_back(x);
  priority_queue<long long int, vector<long long int>, greater<long long int> >
      Q;
  Q.push(1);
  while (Q.empty() == false) {
    long long int cur = Q.top();
    Q.pop();
    if (cur > h || sp[cur % k] != -1) continue;
    sp[cur % k] = cur;
    for (auto i : edges) Q.push(i + cur);
  }
  for (int i = 1; i <= n; i++)
    if (mark[i] == false && sp[a[i] % k] != -1 && sp[a[i] % k] <= a[i])
      s.insert({c[i], -i});
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> h >> n >> m >> k;
  for (int i = 1; i <= n; i++) cin >> a[i] >> c[i];
  add(k);
  while (m--) {
    int typ;
    cin >> typ;
    if (typ == 1) {
      long long int x;
      cin >> x;
      add(x);
    } else if (typ == 2) {
      int x, y;
      cin >> x >> y;
      it = s.find({c[x], -x});
      if (it != s.end()) {
        s.erase(it);
        c[x] -= y;
        s.insert({c[x], -x});
      } else
        c[x] -= y;
    } else {
      if (s.empty())
        cout << "0\n";
      else {
        it = s.end();
        it--;
        cout << it->first << "\n";
        mark[-it->second] = true;
        s.erase(it);
      }
    }
  }
}
