1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include <bits/stdc++.h>
using namespace std;

// loops
#define FOR_3(i, a, b) for (int i = (a), bb##i = (b); i <= bb##i; ++i)
#define FORR_3(i, a, b) for (int i = (b), aa##i = (a); i >= aa##i; --i)

#define FOR_2(i, n) FOR_3(i, 0, (n)-1)
#define FORR_2(i, n) FORR_3(i, 0, (n)-1)

#define FOR_1(n) FOR_2(_i##__LINE__, n)
#define FOR_0() for (;;)

#define OVERLOAD(_0, _1, _2, _3, NAME, ...) NAME
#define FOR(...)                                                               \
  OVERLOAD(_0 __VA_OPT__(, )##__VA_ARGS__, FOR_3, FOR_2, FOR_1, FOR_0)         \
  (__VA_ARGS__)

#define FORR(...)                                                              \
  OVERLOAD(_0 __VA_OPT__(, )##__VA_ARGS__, FORR_3, FORR_2, FORR_1, FOR_0)      \
  (__VA_ARGS__)

// in - expr style
template <class T> T read() {
  T x;
  cin >> x;
  return x;
}
#define II read<int>()
#define ILL read<ll>()
#define ISTR read<string>()

// in - macro style
void _I() {}
template <class ARG, class... ARGS> void _I(ARG &&arg, ARGS &&...args) {
  cin >> arg;
  _I(forward<ARGS>(args)...);
}
#define I(...) _I(__VA_ARGS__);

// out
template <class T> void _print(T x, ostream &os = cout) {
  os << x;
  bool space = true;
  if constexpr (is_same_v<T, char>)
    if (x == '\n')
      space = false;

  if (space)
    os << " ";
}
// template <class T> void _print(const vector<T> &d, ostream &os = cout) {
//   os << '[';
//   for (auto &e : d)
//     os << e << " ";
//   os << ']';
// }
void _O() {}
template <class ARG, class... ARGS> void _O(ARG &&arg, ARGS &&...args) {
  _print(arg);
  _O(forward<ARGS>(args)...);
}
#define O(...) _O(__VA_ARGS__ __VA_OPT__(, ) '\n');
#define OO(...) _O(__VA_ARGS__);

// err
const int _indentSize = 2;
string _indent;

void _E() { cerr << endl; }
template <class ARG, class... ARGS> void _E(ARG &&arg, ARGS &&...args) {
  _print(forward<ARG>(arg), cerr);
  _E(std::forward<ARGS>(args)...);
}

#define ERR(...)                                                               \
  {                                                                            \
    cerr << _indent << "[" << __LINE__ << "] ";                                \
    _E(__VA_ARGS__);                                                           \
  }

#define _V5(x) #x, "==", (x)
#define _V4(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V5(__VA_ARGS__))
#define _V3(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V4(__VA_ARGS__))
#define _V2(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V3(__VA_ARGS__))
#define _V1(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V2(__VA_ARGS__))
#define ERRV(...) ERR(_V1(__VA_ARGS__))
#define INDENT auto _indenter = Indenter();

struct Indenter {
  Indenter() {
    FOR(_indentSize)
    _indent.push_back(' ');
  }
  ~Indenter() {
    FOR(_indentSize)
    _indent.pop_back();
  }
};

#ifdef DEBUG
#define FAIL(str)                                                              \
  {                                                                            \
    cerr << "ASSERT FAILED   " << str << "   " << __FILE__ << ':' << __LINE__  \
         << endl;                                                              \
    abort();                                                                   \
  }
#else
#define FAIL(x)                                                                \
  {}
#endif

// asserts
#define CHECK(cond)                                                            \
  if (!(cond))                                                                 \
  FAIL(#cond)

#define CHECK_OP(op, a, b, res)                                                \
  if (!(res))                                                                  \
  FAIL(#a " " op " " #b "   (" + to_string(a) + " vs " + to_string(b) + ")")

#define CHECK_EQ(a, b) CHECK_OP("==", a, b, (a) == (b))
#define CHECK_NE(a, b) CHECK_OP("!=", a, b, (a) != (b))
#define CHECK_LE(a, b) CHECK_OP("<=", a, b, (a) <= (b))
#define CHECK_GE(a, b) CHECK_OP(">=", a, b, (a) >= (b))
#define CHECK_LT(a, b) CHECK_OP("<", a, b, (a) < (b))
#define CHECK_GT(a, b) CHECK_OP(">", a, b, (a) > (b))

#define CHECK_RANGE(x, a, b)                                                   \
  if (!(a <= x && x < b))                                                      \
  FAIL(#x " in [" #a "," #b ")")

//

#define SZ(v) ((int)(v).size())

using pii = pair<int, int>;

//

template <class T = int> struct Vec : deque<T> {
  int x0 = 0;

  auto &add(const T &el) {
    this->push_back(el);
    return this->back();
  }

  auto &addf(const T &el) {
    this->push_front(el);
    return this->front();
  }

  auto pop() {
    auto r = this->back();
    this->pop_back();
    return r;
  }

  auto popf() {
    auto r = this->front();
    this->pop_front();
    return r;
  }

  auto &operator[](int x) {
    if (x < x0) {
      this->insert(this->begin(), x0 - x, T());
      x0 = x;
    }
    x -= x0;
    if (x >= (int)this->size()) {
      this->insert(this->end(), x - this->size() + 1, T());
    }
    return deque<T>::operator[](x);
  }

  //

  auto &read(int n) {
    FOR(n)
    this->add(::read<T>());
    return *this;
  }
};

template <class T = int> Vec<T> readVec(int n) { return Vec<T>().read(n); }

////////////////////////////////////////////////////////////////////////////////

using ll = long long;

//

int in_n, in_k, in_m;

struct ByColor {
  int color;

  struct Zelka {
    int mass;
    int cost;
  };

  vector<Zelka> zelkis;
};

vector<ByColor> by_color;

const size_t MAX_N = 7'009;
struct Dp : array<ll, MAX_N> {
  Dp() { FOR(i, MAX_N)(*this)[i] = LLONG_MAX; }
};

// struct Dp : array<array<ll, MAX_N>, MAX_N> {
//   Dp() { FOR(i, MAX_N) FOR(j, MAX_N)(*this)[i][j] = -1; }
// };
// mass, reminder -> cost

// mass% -> cost

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  in_n = II;
  in_k = II;
  in_m = II;

  by_color.resize(in_k);

  FOR(in_n) {
    auto ki = II - 1;
    auto mi = II;
    auto ci = II;

    by_color[ki].zelkis.push_back({mi, ci});
  }

  Dp dp_step;
  dp_step[0] = 0;

  FOR(color, in_k) {
    Dp dp_next;

    for (auto &zelka : by_color[color].zelkis) {
      FOR(mass, in_m) {
        if (dp_step[mass] == LLONG_MAX)
          continue;

        auto next_mass = mass + zelka.mass;
        if (next_mass >= in_m)
          next_mass -= in_m;

        dp_next[next_mass] =
            min(dp_next[next_mass], dp_step[mass] + zelka.cost);
      }
    }

    dp_step = std::move(dp_next);
  }

  //

  Dp dp;
  dp[0] = 0;

  FOR(mass, in_m) {
    auto cost = dp_step[mass];

    if (cost == LLONG_MAX)
      continue;

    vector<int> vis(in_m);

    for (int start_idx = 0; start_idx < in_m; ++start_idx) {
      if (dp[start_idx] == LLONG_MAX || vis[start_idx])
        continue;

      int num_loops = 0;

      for (int idx = start_idx;;) {
        vis[idx] = 1;

        auto next_idx = (idx + mass) % in_m;

        dp[next_idx] = min(dp[next_idx], dp[idx] + cost);

        //
        idx = next_idx;
        if (idx == start_idx) {
          ++num_loops;
          if (num_loops == 2)
            break;
        }
      }
    }
  }

  FOR(m, in_m) {
    if (dp[m] == LLONG_MAX) {
      O(-1);
    } else {
      O(dp[m]);
    }
  }

  return 0;
}