1383_F. Special Edges

Koa the Koala has a directed graph G with n nodes and m edges. Each edge has a capacity associated with it. Exactly k edges of the graph, numbered from 1 to k, are special, such edges initially have a capacity equal to 0.

Koa asks you q queries. In each query she gives you k integers w_1, w_2, …, w_k. This means that capacity of the i-th special edge becomes w_i (and other capacities remain the same).

Koa wonders: what is the [maximum flow](https://en.wikipedia.org/wiki/Maximum_flow_problem#Definition) that goes from node 1 to node n after each such query?

Help her!

Input

The first line of the input contains four integers n, m, k, q (2 ≤ n ≤ 10^4, 1 ≤ m ≤ 10^4, 1 ≤ k ≤ min(10, m), 1 ≤ q ≤ 2 ⋅ 10^5) — the number of nodes, the number of edges, the number of special edges and the number of queries.

Each of the next m lines contains three integers u, v, w (1 ≤ u, v ≤ n; 0 ≤ w ≤ 25) — the description of a directed edge from node u to node v with capacity w.

Edges are numbered starting from 1 in the same order they are listed in the input. The first k edges are the special edges. It is guaranteed that w_i = 0 for all i with 1 ≤ i ≤ k.

Each of the next q lines contains k integers w_1, w_2, …, w_k (0 ≤ w_i ≤ 25) — the description of the query. w_i denotes the capacity of i-th edge.

Output

For the i-th query, print one integer res_i — the maximum flow that can be obtained from node 1 to node n given the i-th query's special edge weights.

Examples

Input


2 1 1 3
1 2 0
0
1
2


Output


0
1
2


Input


4 4 2 5
1 2 0
2 3 0
2 4 5
3 4 2
0 0
1 10
10 0
7 1
7 2


Output


0
1
5
6
7

Note

For the second sample, the following images correspond to the first two queries (from left to right respectively). For each edge there is a pair flow/capacity denoting flow pushed through the edge and edge's capacity. The special edges are colored in red.

<image>

As you can see in first query maximum flow from node 1 to node 4 equals 0 and in second query equals 1.

Example input:
4 4 2 5
1 2 0
2 3 0
2 4 5
3 4 2
0 0
1 10
10 0
7 1
7 2

2 1 1 3
1 2 0
0
1
2


Example output:
0
1
5
6
7

0
1
2


Tags: flows
graphs
Difficulty rating: 3200

Solution:
#include <bits/stdc++.h>
int min(int a, int b) { return a < b ? a : b; }
int ij[10000], cc[10000 * 2], m;
int *oh[10000], oo[10000], n;
void link(int i, int h) {
  int o = oo[i]++;
  if (o >= 2 && (o & o - 1) == 0)
    oh[i] = (int *)realloc(oh[i], o * 2 * sizeof *oh[i]);
  oh[i][o] = h;
}
int dd[10000];
int bfs(int s, int t) {
  static int qu[10000];
  int i, head, cnt;
  for (i = 0; i < n; i++) dd[i] = n;
  head = cnt = 0;
  dd[s] = 0, qu[head + cnt++] = s;
  while (cnt) {
    int d, o;
    i = qu[cnt--, head++], d = dd[i] + 1;
    for (o = 0; o < oo[i]; o++) {
      int h_ = oh[i][o];
      if (cc[h_]) {
        int h = h_ >> 1, j = i ^ ij[h];
        if (dd[j] > d) {
          dd[j] = d;
          if (j == t) return 1;
          qu[head + cnt++] = j;
        }
      }
    }
  }
  return 0;
}
int dfs(int i, int t, int c) {
  int d, o;
  if (i == t) return c;
  d = dd[i] + 1;
  for (o = 0; o < oo[i]; o++) {
    int h_ = oh[i][o];
    if (cc[h_]) {
      int h = h_ >> 1, j = i ^ ij[h];
      if (dd[j] == d) {
        int f = dfs(j, t, min(c, cc[h_]));
        if (f) {
          cc[h_] -= f, cc[h_ ^ 1] += f;
          return f;
        }
      }
    }
  }
  dd[i] = n;
  return 0;
}
int dinic(int s, int t) {
  int sum = 0;
  while (bfs(s, t)) {
    int f;
    while ((f = dfs(s, t, 26))) sum += f;
  }
  return sum;
}
int ccc[10][10000 * 2];
int cut[1 << 10], k;
void solve(int b, int h_, int s) {
  int h;
  s += dinic(0, n - 1);
  cut[b] = s;
  if (h_ < k) {
    memcpy(ccc[h_], cc, m * 2 * sizeof *cc);
    for (h = h_; h < k; h++) {
      cc[h << 1] = 26;
      solve(b | 1 << h, h + 1, s);
      memcpy(cc, ccc[h_], m * 2 * sizeof *ccc[h_]);
    }
  }
}
int main() {
  static int ww_[10];
  int q, h, i, j;
  scanf("%d%d%d%d", &n, &m, &k, &q);
  for (i = 0; i < n; i++) oh[i] = (int *)malloc(2 * sizeof *oh[i]);
  for (h = 0; h < m; h++) {
    int c;
    scanf("%d%d%d", &i, &j, &c), i--, j--;
    ij[h] = i ^ j, cc[h << 1] = c;
    link(i, h << 1), link(j, h << 1 | 1);
  }
  solve(0, 0, 0);
  while (q--) {
    int b, w, ans;
    w = 0;
    for (h = 0; h < k; h++) {
      scanf("%d", &ww_[h]);
      w += ww_[h];
    }
    ans = cut[0] + w;
    for (b = 1; b < 1 << k; b++) {
      for (h = 0; (b & 1 << h) == 0; h++) w += ww_[h];
      w -= ww_[h];
      ans = min(ans, cut[b] + w);
    }
    printf("%d\n", ans);
  }
  return 0;
}