#include<iostream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<numeric>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<cctype>
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int NINF = -INF - 1;
const int maxn = 1e5 + 5;
typedef long long ll;
#define MOD 1000000007
using namespace std;
int n, m, st;
int vis[maxn];
int rec = 0;
typedef pair<int, int> P;
struct node
{
int next, cost;
int flag;
};
vector<node> G[maxn];
void bfs()
{
queue<P> q;
q.push(P(1, st));
while(q.size())
{
P p = q.front();
q.pop();
for(int i = 0; i < G[p.first].size(); ++i)
{
node tmp = G[p.first][i];
int mp;
if(!tmp.flag) mp = p.second - tmp.cost;
else mp = p.second + tmp.cost;
if (vis[tmp.next] == -1) {
vis[tmp.next] = mp;
q.push(P(tmp.next, vis[tmp.next]));
}
else{
if(vis[tmp.next] != mp) {
rec = 1;
break;
}
}
}
if(rec) break;
}
}
int main()
{
memset(vis, -1, sizeof(vis));
scanf("%d %d %d", &n, &m, &st);
for(int i = 0; i < m; ++i)
{
int x, y, w;
scanf("%d %d %d", &x, &y, &w);
G[x].push_back(node{y, w, 0});
G[y].push_back(node{x, w, 1});
}
vis[1] = st;
bfs();
// for(int i = 1; i <= n; ++i)
// {
// cout << vis[i] << ' ';
// }
// cout << endl;
for(int i = 1; i <= n; ++i)
{
if(vis[i] == -1)
{
rec = 1;
break;
}
}
if(rec)
{
cout << "QAQ";
}
else
{
cout << "QWQ" << endl;
for(int i = 1; i <= n; ++i)
{
cout << vis[i] << endl;
}
}
return 0;
}