#include <bits/stdc++.h>   using namespace std;   int q[1000005];   long long i, j, t, tot, C, cnt;   long long loc[10005], op[10005];   long long n, m, k, H, x, y, ans, tmp;   long long a[100005], w[100005], e[25], dis[10005], heap[10005];   bool used[100005], f[10005];   map<long long, long long> A;   map<long long, long long>::iterator it;   void Spfa(long long s) {     long long i, head, tail, x, v;     for (i = 1; i <= k; i++) {       dis[i] = 1999999999999999999LL;       f[i] = false;     }     dis[s + 1] = 1;     f[s + 1] = true;     q[1] = s + 1;     head = 1;     tail = 2;     while (head != tail) {       x = q[head];       f[x] = false;       for (i = 1; i <= tot; i++)         if (dis[x] + e[i] < dis[(dis[x] + e[i]) % k + 1]) {           v = (dis[x] + e[i]) % k + 1;           dis[v] = dis[x] + e[i];           if (!f[v]) {             q[tail] = v;             f[v] = true;             tail++;           }         }       head++;     }   }   int main() {     scanf( %I64d%I64d%I64d%I64d , &H, &n, &m, &k);     for (i = 1; i <= n; i++) scanf( %I64d%I64d , &a[i], &w[i]);     tot = 0;     memset(used, false, sizeof used);     x = 1 % k;     for (i = 1; i <= n; i++) {       if (a[i] % k == x) {         used[i] = true;         it = A.find(w[i]);         if (it == A.end())           A.insert(make_pair(w[i], 1));         else           A[w[i]]++;       }     }     for (t = 1; t <= m; t++) {       scanf( %I64d , &C);       if (C == 1) {         scanf( %I64d , &x);         e[++tot] = x;         Spfa(1 % k);         for (i = 1; i <= n; i++)           if (dis[a[i] % k + 1] <= a[i]) {             if (!used[i]) {               used[i] = true;               it = A.find(w[i]);               if (it == A.end())                 A.insert(make_pair(w[i], 1));               else                 A[w[i]]++;             }           }       } else if (C == 2) {         scanf( %I64d%I64d , &x, &y);         if (used[x]) {           it = A.find(w[x]);           tmp = it->second;           if (tmp == 1)             A.erase(w[x]);           else             A[w[x]]--;           w[x] -= y;           it = A.find(w[x]);           if (it != A.end())             A[w[x]]++;           else             A.insert(make_pair(w[x], 1));         } else           w[x] -= y;       } else {         if (A.size() == 0) {           printf( 0 n );           continue;         }         it = A.end();         it--;         ans = it->first;         printf( %I64d n , ans);         tmp = it->second;         if (tmp == 1)           A.erase(ans);         else           A[ans]--;       }     }     return 0;   }   