#include <bits/stdc++.h>
using namespace std;
#define maxn 200010
#define pii pair<int,int>
#define mp make_pair
#define F first
#define S second
#define old _old
#define new _new
pair <int, pii> a[maxn];
int typ[maxn];
int par[maxn], val[maxn];
vector <int> adj[maxn], cd[maxn];
map <int, int> cost[maxn];
int root (int v){
if (par[v]==v) return v;
return par[v]= root(par[v]);
}
void merge (int u, int v){
u= root(u);
v= root(v);
if (u==v) return;
par[u]= v;
}
void dfs (int v, int p){
par[v]= p;
for (int i=0; i<adj[v].size(); i++){
int nt= adj[v][i];
if (nt==p) continue;
cd[v].push_back(adj[v][i]);
val[adj[v][i]]= cost[v][adj[v][i]];
dfs(adj[v][i], v);
}
}
multiset <int> ans[maxn];
multiset <int> ovr;
set <int> cols[maxn];
multiset <int> st[2*maxn];
map <int, int> col[maxn];
void print (){
printf("%d\n", (*ovr.begin()));
}
int cur;
int get (int v, int cl){
int q= col[v][cl];
if (q==0){
cur++;
col[v][cl]= cur;
q= cur;
}
return q;
}
int main(){
freopen ("grass.in", "r", stdin);
freopen ("grass.out", "w", stdout);
cur= 0;
int n, m, k, q;
scanf("%d %d %d %d", &n, &m ,&k, &q);
for (int i=1; i<=m; i++){
//cin>>a[i].S.F>>a[i].S.S>>a[i].F;
scanf("%d %d %d", &a[i].S.F, &a[i].S.S, &a[i].F);
}
sort(a+1, a+m+1);
for (int i=1; i<=n; i++){
//cin>>typ[i];
scanf("%d ", &typ[i]);
par[i]= i;
}
for (int i=1; i<=m; i++){
int u= a[i].S.F, v= a[i].S.S;
if (root(u)==root(v)) continue;
merge(u, v);
adj[u].push_back(v);
adj[v].push_back(u);
cost[u][v]= cost[v][u]= a[i].F;
}
dfs(1, 0);
for (int i=1; i<=n; i++){
for (int j=0; j<cd[i].size(); j++){
int nt= cd[i][j];
int tp= get(i, typ[nt]);
st[tp].insert(val[nt]);
cols[i].insert(typ[nt]);
}
for (set <int> :: iterator it= cols[i].begin(); it!=cols[i].end(); it++){
int t= *it;
if (t!=typ[i]){
int tp= get(i, t);
ans[i].insert(*st[tp].begin());
}
}
}
for (int i=1; i<=n; i++) if (!ans[i].empty()) ovr.insert(*ans[i].begin());
while (q--){
int u, v;
scanf("%d %d", &u, &v);
if (typ[u]==v){
print();
continue;
}
int old= -1;
if (ans[u].size()>0) old= *ans[u].begin();
int del= -1;
int tp= get(u, v);
if (st[tp].size()>0) del= *st[tp].begin();
int add= -1;
tp= get(u, typ[u]);
if (st[tp].size()>0) add= *st[tp].begin();
if (del!=-1) {
ans[u].erase(ans[u].find(del));
}
if (add!=-1) {
ans[u].insert(add);
}
int new= -1;
if (!ans[u].empty()) new= *ans[u].begin();
if (old!=-1) ovr.erase(ovr. find(old));
if (new!=-1) ovr.insert(new);
if (u==1) {
typ[u]= v;
print();
continue;
}
int p= par[u];
tp= get(p, typ[u]);
old= *st[tp].begin();
int old2=-1;
if (!ans[p].empty()) old2= *ans[p].begin();
int old3= -1;
tp= get(p, typ[u]);
if (typ[u]!=typ[p]) old3= *st[tp].begin();
st[tp].erase(st[tp].find(val[u]));
if (old3!=-1){
ans[p].erase(ans[p].find(old3));
if (!st[tp].empty()) {
ans[p].insert(*st[tp].begin());
}
}
int old4= -1;
tp= get(p, v);
if (typ[p]!=v) {
if (!st[tp].empty()) old4= *st[tp].begin();
}
st[tp].insert(val[u]);
if (typ[p]!=v){
if (old4!=-1) {
ans[p].erase(ans[p].find(old4));
}
ans[p].insert(*st[tp].begin());
}
if (old2!=-1) ovr.erase(ovr.find(old2));
if (!ans[p].empty()) ovr.insert(*ans[p].begin());
typ[u]= v;
print();
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgbWF4biAyMDAwMTAKI2RlZmluZSBwaWkgcGFpcjxpbnQsaW50PgojZGVmaW5lIG1wIG1ha2VfcGFpcgojZGVmaW5lIEYgZmlyc3QKI2RlZmluZSBTIHNlY29uZAojZGVmaW5lIG9sZCBfb2xkCiNkZWZpbmUgbmV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