#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  T flag = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') flag = -1;
  for (x = 0; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  x *= flag;
}
const int maxn = 100005;
struct e {
  int u, v, val;
} ans[maxn];
int n, m;
char s[maxn];
int col[maxn], f[maxn], val[maxn], d[maxn];
vector<pair<int, int>> vec[maxn], t[maxn];
bool vis[maxn];
int tmp[maxn];
int flag, End;
int pre[maxn], num[maxn];
void print(int x, int y, int z) { printf("%d %d %d\n", x, y, z); }
void dfs(int x, int fa, int id) {
  f[x] = fa;
  if (fa) t[x].push_back(make_pair(fa, id));
  for (int i = 0; i < (int)vec[x].size(); i++) {
    int y = vec[x][i].first;
    if (f[y] > 0 || y == flag) continue;
    t[x].push_back(make_pair(y, vec[x][i].second));
    dfs(y, x, vec[x][i].second);
  }
}
void get_ans(int x) {
  for (int i = 0; i < (int)t[x].size(); i++) {
    int y = t[x][i].first;
    if (y == f[x]) continue;
    get_ans(y);
    int m = col[y] - d[y];
    ans[t[x][i].second].val = m;
    d[y] = d[y] + m;
    d[x] = d[x] + m;
  }
}
void get_ans2(int x) {
  for (int i = 0; i < (int)t[x].size(); i++) {
    int y = t[x][i].first;
    if (y == f[x]) continue;
    get_ans2(y);
    int m = tmp[y] - d[y];
    ans[t[x][i].second].val = m;
    d[y] = d[y] + m;
    d[x] = d[x] + m;
  }
}
void check(int x) {
  for (int i = 0; i < (int)vec[x].size(); i++) {
    int y = vec[x][i].first;
    if (tmp[y] == -1)
      tmp[y] = tmp[x] ^ 1, check(y);
    else {
      if (tmp[y] == tmp[x]) {
        flag = x;
      }
    }
  }
}
void yyy(int x) {
  for (int i = 0; i < (int)vec[x].size(); i++) {
    int y = vec[x][i].first;
    if (tmp[y] != -1) continue;
    tmp[y] = tmp[x] ^ 1;
    yyy(y);
  }
}
int cnt[maxn];
void output() {
  for (int i = 1; i <= m; i++) {
    ans[i].val = (ans[i].val % 3 + 3) % 3;
    if (ans[i].val == 0) ans[i].val = 3;
    print(ans[i].u, ans[i].v, ans[i].val);
    cnt[ans[i].u] += ans[i].val;
    cnt[ans[i].v] += ans[i].val;
  }
}
void jjj(int x) {
  for (int i = 0; i < (int)vec[x].size(); i++) {
    int y = vec[x][i].first;
    if (tmp[y] == -1)
      tmp[y] = tmp[x] ^ 1, pre[y] = x, num[y] = vec[x][i].second, jjj(y);
    else {
      if (y == flag && tmp[x] == tmp[flag]) {
        End = x;
      }
    }
  }
}
void work() {
  tmp[1] = 0;
  check(1);
  if (flag) {
    dfs(flag, 0, 0);
    get_ans(flag);
    int m = (col[flag] - d[flag]);
    memset(tmp, -1, sizeof(tmp));
    tmp[flag] = 0;
    jjj(flag);
    for (int i = End; i != flag; i = pre[i]) {
      ans[num[i]].val += m;
      m = -m;
    }
    for (int i = 0; i < (int)vec[End].size(); i++) {
      if (vec[End][i].first == flag) ans[vec[End][i].second].val += m << 1;
    }
    output();
  } else {
    flag = 1;
    memset(tmp, -1, sizeof(tmp));
    tmp[1] = 0;
    yyy(flag);
    dfs(flag, 0, 0);
    get_ans2(flag);
    if ((d[1] % 3 + 3) % 3 == 1 && vec[1].size() > 1) {
      ans[vec[1][1].second].val++;
      ans[vec[1][2].second].val++;
    }
    output();
  }
}
int main() {
  memset(tmp, -1, sizeof(tmp));
  read(n);
  read(m);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) {
    col[i] = s[i] - 'X';
  }
  for (int i = 1; i <= m; i++) {
    int u, v;
    read(u);
    read(v);
    ans[i].u = u;
    ans[i].v = v;
    u++;
    v++;
    vec[u].push_back(make_pair(v, i));
    vec[v].push_back(make_pair(u, i));
  }
  for (int i = 1; i <= n; i++) reverse(vec[i].begin(), vec[i].end());
  work();
  return 0;
}
