#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const long long maxn = 1e5 + 10, inf = 1e9, mod = 1e9 + 7;
const long double pi = 3.14159265359, eps = 1e-9;
const long long maxk = 1e4 + 10;
long long h, n, m, k, v, u, w;
vector<pair<long long, long long> > g[maxk];
long long a[maxn], c[maxn];
bool mk[maxn];
set<pair<long long, long long> > tr, dij;
long long dis[maxk];
void DJKASRA() {
  for (int i = 0; i < k; i++) dij.insert({dis[i], i});
  while (((long long)dij.size())) {
    v = dij.begin()->second;
    dij.erase(dij.begin());
    for (pair<long long, long long> e : g[v]) {
      u = e.first;
      w = e.second;
      if (dis[u] > dis[v] + w) {
        dis[u] = dis[v] + w;
        dij.insert({dis[u], u});
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (mk[i]) continue;
    if (dis[a[i] % k] <= a[i]) {
      mk[i] = 1;
      tr.insert({-c[i], i});
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  memset(dis, 31, sizeof(dis));
  dis[1] = 1;
  cin >> h >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> c[i];
    if (a[i] % k == 1) mk[i] = 1, tr.insert({-c[i], i});
  }
  long long ty, x, y;
  while (m--) {
    cin >> ty;
    if (ty == 1) {
      cin >> x;
      for (int i = 0; i < k; i++) g[i].push_back({(i + x) % k, x});
      DJKASRA();
    } else if (ty == 2) {
      cin >> x >> y;
      if (tr.count({-c[x], x})) tr.erase({-c[x], x}), tr.insert({-c[x] + y, x});
      c[x] -= y;
    } else {
      if (!((long long)tr.size()))
        cout << 0 << "\n";
      else {
        cout << -(tr.begin()->first) << "\n";
        tr.erase(tr.begin());
      }
    }
  }
  return 0;
}
