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