#include <bits/stdc++.h>
using namespace std;
int i, i0, n, m, c, q, pre[100005];
map<int, vector<int>> mp[100005];
set<int> dic[100005];
int fin(int x) { return (pre[x] == x) ? x : pre[x] = fin(pre[x]); }
void uni(int x, int y) {
  if (fin(x) != fin(y)) {
    if (dic[fin(x)].size() < dic[fin(y)].size()) swap(x, y);
    for (auto i : dic[fin(y)]) dic[fin(x)].insert(i), pre[fin(y)] = fin(x);
  }
}
void add() {
  int x, y, col;
  scanf("%d %d %d", &x, &y, &col);
  dic[fin(x)].insert(y), dic[fin(y)].insert(x);
  mp[x][col].push_back(y), uni(y, mp[x][col][0]);
  mp[y][col].push_back(x), uni(x, mp[y][col][0]);
}
int main() {
  scanf("%d %d %d %d", &n, &m, &c, &q);
  for (i = 1; i <= n; i++) pre[i] = i;
  while (m--) add();
  while (q--) {
    char op;
    scanf(" %c", &op);
    if (op == '?') {
      int x, y;
      scanf("%d %d", &x, &y);
      printf("%s\n", (fin(x) == fin(y) || dic[fin(x)].count(y)) ? "Yes" : "No");
    } else
      add();
  }
  return 0;
}
