#include <bits/stdc++.h>
using namespace std;
#define task "main"
#define no "NO"
#define yes "YES"
#define F first
#define S second
#define vec vector
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
const int MAX_N = (int)3e5 + 5;
const int MAX_M = (int)9e5 + 5;
int numNode, numEdge, A, B;
ii nodes[MAX_N];
vector<ii> adj[MAX_N];
namespace sub1 {
    bool valid() {
        return max(numNode, numEdge) <= 6000;
    }
    const int MAX_N = (int)6e3 + 3;
    bool vis[MAX_N];
    int ans = 0;
    void dfs(int u) {
        if (nodes[u].F == A) ans++;
        for (ii e : adj[u]) if (!vis[e.S]) {
            vis[e.S] = 1;
            dfs(e.F);
        }
    }
    bool cmp(ii &x, ii &y) {
        return x.S > y.S;
    }
    void solve() {
        vector<ii> res;
        FOR(i, 1, numNode) if (nodes[i].F == 0) {
            memset(vis, 0, sizeof(vis));
            ans = 0;
            dfs(i);
            res.push_back({-nodes[i].S, ans});
        }
        sort(res.begin(), res.end());
        for (ii x : res) cout << x.S << "\n";
    }
}
void solve() {
    cin >> numNode >> numEdge >> A >> B;
    FOR(i, 1, numNode) cin >> nodes[i].F >> nodes[i].S;
    FOR(i, 1, numEdge) {
        int u, v, k;
        cin >> u >> v >> k;
        adj[u].push_back({v, i});
        if (k == 2) adj[v].push_back({u, i});
    }
    if (sub1::valid()) sub1::solve();
}
int32_t main() {
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    bool multitest = 0;
    int numTest = 1;
    if (multitest) cin >> numTest;
    while (numTest--) {
        solve();
    }
    return 0;
}
//5 3 1 3
//0 0
//0 1
//0 2
//1 0
//1 1
//1 4 1
//1 5 2
//3 5 2
/* Lak lu theo dieu nhac!!!! */
Compilation message (stderr)
tra.cpp: In function 'int32_t main()':
tra.cpp:75:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tra.cpp:76:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |