#include <bits/stdc++.h>
using namespace std;
long long n, m, k;
long long h;
const long long maxn = 1010101;
long long a[maxn], d[maxn], data[maxn], ed[maxn], c[maxn];
long long l;
struct Node {
  long long c, num;
  Node() {}
  Node(long long _c, long long _num) {
    c = _c;
    num = _num;
  }
  bool operator<(const Node &b) const {
    if (c == b.c) return num > b.num;
    return c < b.c;
  }
};
bool vis[maxn];
priority_queue<Node> Q;
const long long oo = 1010101010101010LL;
queue<long long> q;
int main() {
  std::ios::sync_with_stdio(false);
  cin >> h >> n >> m >> k;
  for (long long i = 0; i <= maxn; i++) d[i] = oo;
  d[1] = 0;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i] >> c[i];
    if ((a[i] % k == 1)) Q.push(Node(c[i], i));
  }
  for (long long j = 1; j <= m; j++) {
    long long t;
    cin >> t;
    if (t == 2) {
      long long x, y;
      cin >> x >> y;
      c[x] -= y;
      if (d[a[x] % k] <= a[x] / k) Q.push(Node(c[x], x));
    } else if (t == 3) {
      while (1) {
        if (Q.empty()) {
          cout << 0 << endl;
          break;
        }
        Node now = Q.top();
        Q.pop();
        if (c[now.num] == now.c) {
          cout << now.c << endl;
          c[now.num] = 0;
          break;
        }
      }
    } else {
      long long z;
      cin >> z;
      data[++l] = z / k;
      ed[l] = z % k;
      for (long long i = 1; i <= k; i++) {
        q.push(i - 1);
      }
      while (!q.empty()) {
        long long now = q.front();
        q.pop();
        vis[now] = false;
        for (long long i = 1; i <= l; i++) {
          long long y = now + ed[i];
          z = d[now] + data[i];
          if (y >= k) {
            y -= k;
            z++;
          }
          if (z < d[y]) {
            d[y] = z;
            if (!vis[y]) {
              q.push(y);
              vis[y] = true;
            }
          }
        }
      }
      while (!Q.empty()) Q.pop();
      for (int i = 1; i <= n; i++)
        if (c[i] && d[a[i] % k] <= a[i] / k) Q.push(Node(c[i], i));
    }
  }
  return 0;
}
