function PriorityQueue(compare) {
  this.heap = [];
  this.elements = 0;
  this.compare = compare || ((a, b) => a < b);
}

PriorityQueue.prototype.size = function () {
  return this.elements;
};

PriorityQueue.prototype.isEmpty = function () {
  return this.elements == 0;
};

PriorityQueue.prototype.clear = function () {
  this.elements = 0;
};

PriorityQueue.prototype.peek = function () {
  return this.elements > 0 ? heap[0] : undefined;
};

PriorityQueue.prototype.add = function (myVal) {
  this.heap[this.elements++] = myVal;

  var current = this.elements - 1;
  while (current > 0) {
    var up = (current - 1) >> 1;
    var upVal = this.heap[up];
    if (this.compare(upVal, myVal)) break;
    this.heap[current] = upVal;
    current = up;
  }
  this.heap[current] = myVal;
};

PriorityQueue.prototype.pop = function () {
  if (this.elements == 0) return null;
  if (this.elements == 1) {
    return this.heap[--this.elements];
  }
  var el = this.heap[0];

  var myVal = this.heap[--this.elements];
  this.heap[0] = myVal;

  var current = 0;
  var maxSize = this.elements / 2;
  var leftChild;
  var rightChild;
  var best;

  while (current < maxSize) {
    leftChild = current * 2 + 1;
    rightChild = leftChild + 1;
    best = this.heap[leftChild];

    if (
      rightChild < this.elements &&
      this.compare(this.heap[rightChild], best)
    ) {
      best = this.heap[rightChild];
      leftChild = rightChild;
    }
    if (this.compare(myVal, best)) {
      break;
    }
    this.heap[current] = best;
    current = leftChild;
  }
  this.heap[current] = myVal;

  return el;
};

PriorityQueue.prototype.toArray = function () {
  return this.heap.filter((_, id) => id < this.elements);
};

var line = readline().split(" ");
var n = parseInt(line[0]);
var m = parseInt(line[1]);

var nodes = Array(n);
var weights = Array(m);
var iniEdges = Array(m);
for (var i = 0; i < m; i++) {
  var edge = readline().split(" ");
  var node1 = parseInt(edge[0]);
  var node2 = parseInt(edge[1]);
  var weight = parseInt(edge[2]) * 2; //*2 to avoid decimals
  addEdge(nodes, node1, node2, weight, i);
  weights[i] = weight;
  iniEdges[i] = [node1, node2, parseInt(edge[2])];
}

var cuts = new Set();
cuts.add(0);
for (var i = 0; i < m; i++) {
  var w1 = weights[i];
  cuts.add(w1);
  for (var j = 0; j < i; j++) {
    cuts.add((w1 + weights[j]) / 2);
  }
}
cuts = Array.from(cuts);
cuts.sort((a, b) => a - b);

var line = readline().split(" ");
var p = parseInt(line[0]);
var k = parseInt(line[1]);
var a = parseInt(line[2]);
var b = parseInt(line[3]);
var c = parseInt(line[4]);

var queries = readline()
  .split(" ")
  .map((i) => parseInt(i));

var edges = new PriorityQueue((a, b) => {
  return a[1] == b[1] ? b[3] < a[3] : a[1] < b[1];
});

var edgeCuts = new Array(cuts.length);
edgeCuts[0] = [mst(edges, nodes, cuts[0]), cuts[0]];
edgeCuts[cuts.length - 1] = [
  mst(edges, nodes, cuts[cuts.length - 1]),
  cuts[cuts.length - 1]
];
if (!sortedArrayEquals(edgeCuts[0][0][0], edgeCuts[cuts.length - 1][0][0]))
  calcCuts(cuts, edgeCuts, edges, 0, cuts.length - 1);
edgeCuts = edgeCuts.filter((i) => i != undefined);
edgeCuts = edgeCuts.filter(
  (i, id) => id == 0 || !sortedArrayEquals(i[0][0], edgeCuts[id - 1][0][0])
);
for (var i = 0; i < weights.length; i++) {
  edgeCuts.push([mst(edges, nodes, weights[i]), weights[i]]);
}
edgeCuts.sort((a, b) => a[1] - b[1]);
edgeCuts = edgeCuts.filter((i, id) => id == 0 || i[1] != edgeCuts[id - 1][1]);

var result = 0;
var q = 0;
var dict={}
for (var i = 0; i < k; i++) {
  var qbase = i < queries.length ? queries[i] : ((q * a) / 2 + b) % c;
  q = qbase * 2;
  var res= dict[q]
  if (dict[q] == undefined){
  var j = upperBoundEdges(edgeCuts, q);

  var edgesList = edgeCuts[j][0][0];
  var weightsList = edgeCuts[j][0][1];
  var z = upperBound(weightsList, q);
  var res =
    (edgeCuts[j][0][2] - (n - 1 - 2 * z) * Math.abs(edgeCuts[j][1] - q)) / 2;}
  result ^= res;
}

print(result<0?result+8*(1<<30):result);

function upperBoundEdges(edgeCuts, q) {
  var min = 0;
  var max = edgeCuts.length;
  var mid = Math.floor((min + max) / 2);
  while (min != mid) {
    if (edgeCuts[mid][1] <= q) min = mid;
    else max = mid;
    mid = Math.floor((min + max) / 2);
  }
  return mid;
}

function upperBound(arr, q) {
  var min = -1;
  var max = arr.length;
  var mid = Math.floor((min + max) / 2);
  while (min != mid) {
    if (arr[mid] <= q) min = mid;
    else max = mid;
    mid = Math.floor((min + max) / 2);
  }
  return mid + 1;
}

function calcCuts(cuts, edgeCuts, edges, ini, end) {
  var mid = Math.floor((ini + end) / 2);
  if (mid == ini) return;
  edgeCuts[mid] = [mst(edges, nodes, cuts[mid]), cuts[mid]];
  var equalDown = sortedArrayEquals(edgeCuts[ini][0][0], edgeCuts[mid][0][0]);
  var equalUp = sortedArrayEquals(edgeCuts[end][0][0], edgeCuts[mid][0][0]);
  if (!equalDown) calcCuts(cuts, edgeCuts, edges, ini, mid);
  if (!equalUp) calcCuts(cuts, edgeCuts, edges, mid, end);
}

function sortedArrayEquals(a, b) {
  for (var i = 0; i < a.length; i++) if (a[i] != b[i]) return false;
  return true;
}

function addEdge(nodes, n1, n2, w, i) {
  if (nodes[n1 - 1] == undefined) nodes[n1 - 1] = [];
  if (nodes[n2 - 1] == undefined) nodes[n2 - 1] = [];

  nodes[n1 - 1].push([n2 - 1, w, i]);
  nodes[n2 - 1].push([n1 - 1, w, i]);
}

function mst(edges, nodes, q) {
  edges.clear();
  var visited = { 0: true };
  var nVisited = 1;
  var edgesOrder = [];
  var weightsOrder = [];
  var sum = 0;
  for (var edge of nodes[0])
    edges.add([edge[0], Math.abs(edge[1] - q), edge[2], edge[1]]);
  while (true) {
    var min = edges.pop();
    var node = min[0];
    if (!visited[node]) {
      visited[node] = true;
      edgesOrder.push(min[2]);
      weightsOrder.push(min[3]);
      sum += min[1];
      if (++nVisited == nodes.length) break;
      for (var edge of nodes[node]) {
        if (!visited[edge[0]])
          edges.add([edge[0], Math.abs(edge[1] - q), edge[2], edge[1]]);
      }
    }
  }
  return [
    edgesOrder.sort((a, b) => a - b),
    weightsOrder.sort((a, b) => a - b),
    sum
  ];
}

function PriorityQueue(compare) {
  this.heap = [];
  this.elements = 0;
  this.compare = compare || ((a, b) => a < b);
}

PriorityQueue.prototype.size = function () {
  return this.elements;
};

PriorityQueue.prototype.isEmpty = function () {
  return this.elements == 0;
};

PriorityQueue.prototype.clear = function () {
  this.elements = 0;
};

PriorityQueue.prototype.peek = function () {
  return this.elements > 0 ? heap[0] : undefined;
};

PriorityQueue.prototype.add = function (myVal) {
  this.heap[this.elements++] = myVal;

  var current = this.elements - 1;
  while (current > 0) {
    var up = (current - 1) >> 1;
    var upVal = this.heap[up];
    if (this.compare(upVal, myVal)) break;
    this.heap[current] = upVal;
    current = up;
  }
  this.heap[current] = myVal;
};

PriorityQueue.prototype.pop = function () {
  if (this.elements == 0) return null;
  if (this.elements == 1) {
    return this.heap[--this.elements];
  }
  var el = this.heap[0];

  var myVal = this.heap[--this.elements];
  this.heap[0] = myVal;

  var current = 0;
  var maxSize = this.elements / 2;
  var leftChild;
  var rightChild;
  var best;

  while (current < maxSize) {
    leftChild = current * 2 + 1;
    rightChild = leftChild + 1;
    best = this.heap[leftChild];

    if (
      rightChild < this.elements &&
      this.compare(this.heap[rightChild], best)
    ) {
      best = this.heap[rightChild];
      leftChild = rightChild;
    }
    if (this.compare(myVal, best)) {
      break;
    }
    this.heap[current] = best;
    current = leftChild;
  }
  this.heap[current] = myVal;

  return el;
};

PriorityQueue.prototype.toArray = function () {
  return this.heap.filter((_, id) => id < this.elements);
};

var line = readline().split(" ");
var n = parseInt(line[0]);
var m = parseInt(line[1]);

var nodes = Array(n);
var weights = Array(m);
var iniEdges = Array(m);
for (var i = 0; i < m; i++) {
  var edge = readline().split(" ");
  var node1 = parseInt(edge[0]);
  var node2 = parseInt(edge[1]);
  var weight = parseInt(edge[2]) * 2; //*2 to avoid decimals
  addEdge(nodes, node1, node2, weight, i);
  weights[i] = weight;
  iniEdges[i] = [node1, node2, parseInt(edge[2])];
}

var cuts = new Set();
cuts.add(0);
for (var i = 0; i < m; i++) {
  var w1 = weights[i];
  cuts.add(w1);
  for (var j = 0; j < i; j++) {
    cuts.add((w1 + weights[j]) / 2);
  }
}
cuts = Array.from(cuts);
cuts.sort((a, b) => a - b);

var line = readline().split(" ");
var p = parseInt(line[0]);
var k = parseInt(line[1]);
var a = parseInt(line[2]);
var b = parseInt(line[3]);
var c = parseInt(line[4]);

var queries = readline()
  .split(" ")
  .map((i) => parseInt(i));

var edges = new PriorityQueue((a, b) => {
  return a[1] == b[1] ? b[3] < a[3] : a[1] < b[1];
});

var edgeCuts = new Array(cuts.length);
edgeCuts[0] = [mst(edges, nodes, cuts[0]), cuts[0]];
edgeCuts[cuts.length - 1] = [
  mst(edges, nodes, cuts[cuts.length - 1]),
  cuts[cuts.length - 1]
];
if (!sortedArrayEquals(edgeCuts[0][0][0], edgeCuts[cuts.length - 1][0][0]))
  calcCuts(cuts, edgeCuts, edges, 0, cuts.length - 1);
edgeCuts = edgeCuts.filter((i) => i != undefined);
edgeCuts = edgeCuts.filter(
  (i, id) => id == 0 || !sortedArrayEquals(i[0][0], edgeCuts[id - 1][0][0])
);
for (var i = 0; i < weights.length; i++) {
  edgeCuts.push([mst(edges, nodes, weights[i]), weights[i]]);
}
edgeCuts.sort((a, b) => a[1] - b[1]);
edgeCuts = edgeCuts.filter((i, id) => id == 0 || i[1] != edgeCuts[id - 1][1]);

var result = 0;
var q = 0;
for (var i = 0; i < k; i++) {
  var qbase = i < queries.length ? queries[i] : ((q * a) / 2 + b) % c;
  q = qbase * 2;
  var j = upperBoundEdges(edgeCuts, q);

  var edgesList = edgeCuts[j][0][0];
  var weightsList = edgeCuts[j][0][1];
  var z = upperBound(weightsList, q);
  var sum2 =
    (edgeCuts[j][0][2] - (n - 1 - 2 * z) * Math.abs(edgeCuts[j][1] - q)) / 2;
  result ^= sum2;
}

print(result<0?result+8*(1<<30):result);

function upperBoundEdges(edgeCuts, q) {
  var min = 0;
  var max = edgeCuts.length;
  var mid = Math.floor((min + max) / 2);
  while (min != mid) {
    if (edgeCuts[mid][1] <= q) min = mid;
    else max = mid;
    mid = Math.floor((min + max) / 2);
  }
  return mid;
}

function upperBound(arr, q) {
  var min = -1;
  var max = arr.length;
  var mid = Math.floor((min + max) / 2);
  while (min != mid) {
    if (arr[mid] <= q) min = mid;
    else max = mid;
    mid = Math.floor((min + max) / 2);
  }
  return mid + 1;
}

function calcCuts(cuts, edgeCuts, edges, ini, end) {
  var mid = Math.floor((ini + end) / 2);
  if (mid == ini) return;
  edgeCuts[mid] = [mst(edges, nodes, cuts[mid]), cuts[mid]];
  var equalDown = sortedArrayEquals(edgeCuts[ini][0][0], edgeCuts[mid][0][0]);
  var equalUp = sortedArrayEquals(edgeCuts[end][0][0], edgeCuts[mid][0][0]);
  if (!equalDown) calcCuts(cuts, edgeCuts, edges, ini, mid);
  if (!equalUp) calcCuts(cuts, edgeCuts, edges, mid, end);
}

function sortedArrayEquals(a, b) {
  for (var i = 0; i < a.length; i++) if (a[i] != b[i]) return false;
  return true;
}

function addEdge(nodes, n1, n2, w, i) {
  if (nodes[n1 - 1] == undefined) nodes[n1 - 1] = [];
  if (nodes[n2 - 1] == undefined) nodes[n2 - 1] = [];

  nodes[n1 - 1].push([n2 - 1, w, i]);
  nodes[n2 - 1].push([n1 - 1, w, i]);
}

function mst(edges, nodes, q) {
  edges.clear();
  var visited = { 0: true };
  var nVisited = 1;
  var edgesOrder = [];
  var weightsOrder = [];
  var sum = 0;
  for (var edge of nodes[0])
    edges.add([edge[0], Math.abs(edge[1] - q), edge[2], edge[1]]);
  while (true) {
    var min = edges.pop();
    var node = min[0];
    if (!visited[node]) {
      visited[node] = true;
      edgesOrder.push(min[2]);
      weightsOrder.push(min[3]);
      sum += min[1];
      if (++nVisited == nodes.length) break;
      for (var edge of nodes[node]) {
        if (!visited[edge[0]])
          edges.add([edge[0], Math.abs(edge[1] - q), edge[2], edge[1]]);
      }
    }
  }
  return [
    edgesOrder.sort((a, b) => a - b),
    weightsOrder.sort((a, b) => a - b),
    sum
  ];
}

function PriorityQueue(compare) {
  this.heap = [];
  this.elements = 0;
  this.compare = compare || ((a, b) => a < b);
}

PriorityQueue.prototype.size = function () {
  return this.elements;
};

PriorityQueue.prototype.isEmpty = function () {
  return this.elements == 0;
};

PriorityQueue.prototype.clear = function () {
  this.elements = 0;
};

PriorityQueue.prototype.peek = function () {
  return this.elements > 0 ? heap[0] : undefined;
};

PriorityQueue.prototype.add = function (myVal) {
  this.heap[this.elements++] = myVal;

  var current = this.elements - 1;
  while (current > 0) {
    var up = (current - 1) >> 1;
    var upVal = this.heap[up];
    if (this.compare(upVal, myVal)) break;
    this.heap[current] = upVal;
    current = up;
  }
  this.heap[current] = myVal;
};

PriorityQueue.prototype.pop = function () {
  if (this.elements == 0) return null;
  if (this.elements == 1) {
    return this.heap[--this.elements];
  }
  var el = this.heap[0];

  var myVal = this.heap[--this.elements];
  this.heap[0] = myVal;

  var current = 0;
  var maxSize = this.elements / 2;
  var leftChild;
  var rightChild;
  var best;

  while (current < maxSize) {
    leftChild = current * 2 + 1;
    rightChild = leftChild + 1;
    best = this.heap[leftChild];

    if (
      rightChild < this.elements &&
      this.compare(this.heap[rightChild], best)
    ) {
      best = this.heap[rightChild];
      leftChild = rightChild;
    }
    if (this.compare(myVal, best)) {
      break;
    }
    this.heap[current] = best;
    current = leftChild;
  }
  this.heap[current] = myVal;

  return el;
};

PriorityQueue.prototype.toArray = function () {
  return this.heap.filter((_, id) => id < this.elements);
};

var line = readline().split(" ");
var n = parseInt(line[0]);
var m = parseInt(line[1]);

var nodes = Array(n);
var weights = Array(m);
var iniEdges = Array(m);
for (var i = 0; i < m; i++) {
  var edge = readline().split(" ");
  var node1 = parseInt(edge[0]);
  var node2 = parseInt(edge[1]);
  var weight = parseInt(edge[2]) * 2; //*2 to avoid decimals
  addEdge(nodes, node1, node2, weight, i);
  weights[i] = weight;
  iniEdges[i] = [node1, node2, parseInt(edge[2])];
}

var cuts = new Set();
cuts.add(0);
for (var i = 0; i < m; i++) {
  var w1 = weights[i];
  cuts.add(w1);
  for (var j = 0; j < i; j++) {
    cuts.add((w1 + weights[j]) / 2);
  }
}
cuts = Array.from(cuts);
cuts.sort((a, b) => a - b);

var line = readline().split(" ");
var p = parseInt(line[0]);
var k = parseInt(line[1]);
var a = parseInt(line[2]);
var b = parseInt(line[3]);
var c = parseInt(line[4]);

var queries = readline()
  .split(" ")
  .map((i) => parseInt(i));

var edges = new PriorityQueue((a, b) => {
  return a[1] == b[1] ? b[3] < a[3] : a[1] < b[1];
});

var edgeCuts = new Array(cuts.length);
edgeCuts[0] = [mst(edges, nodes, cuts[0]), cuts[0]];
edgeCuts[cuts.length - 1] = [
  mst(edges, nodes, cuts[cuts.length - 1]),
  cuts[cuts.length - 1]
];
if (!sortedArrayEquals(edgeCuts[0][0][0], edgeCuts[cuts.length - 1][0][0]))
  calcCuts(cuts, edgeCuts, edges, 0, cuts.length - 1);
edgeCuts = edgeCuts.filter((i) => i != undefined);
edgeCuts = edgeCuts.filter(
  (i, id) => id == 0 || !sortedArrayEquals(i[0][0], edgeCuts[id - 1][0][0])
);
for (var i = 0; i < weights.length; i++) {
  edgeCuts.push([mst(edges, nodes, weights[i]), weights[i]]);
}
edgeCuts.sort((a, b) => a[1] - b[1]);
edgeCuts = edgeCuts.filter((i, id) => id == 0 || i[1] != edgeCuts[id - 1][1]);

var result = 0;
var q = 0;
for (var i = 0; i < k; i++) {
  var qbase = i < queries.length ? queries[i] : ((q * a) / 2 + b) % c;
  q = qbase * 2;
  var j = upperBoundEdges(edgeCuts, q);

  var edgesList = edgeCuts[j][0][0];
  var weightsList = edgeCuts[j][0][1];
  var z = upperBound(weightsList, q);
  var sum2 =
    (edgeCuts[j][0][2] - (n - 1 - 2 * z) * Math.abs(edgeCuts[j][1] - q)) / 2;
  result ^= sum2;
}

print(result);

function upperBoundEdges(edgeCuts, q) {
  var min = 0;
  var max = edgeCuts.length;
  var mid = Math.floor((min + max) / 2);
  while (min != mid) {
    if (edgeCuts[mid][1] <= q) min = mid;
    else max = mid;
    mid = Math.floor((min + max) / 2);
  }
  return mid;
}

function upperBound(arr, q) {
  var min = -1;
  var max = arr.length;
  var mid = Math.floor((min + max) / 2);
  while (min != mid) {
    if (arr[mid] <= q) min = mid;
    else max = mid;
    mid = Math.floor((min + max) / 2);
  }
  return mid + 1;
}

function calcCuts(cuts, edgeCuts, edges, ini, end) {
  var mid = Math.floor((ini + end) / 2);
  if (mid == ini) return;
  edgeCuts[mid] = [mst(edges, nodes, cuts[mid]), cuts[mid]];
  var equalDown = sortedArrayEquals(edgeCuts[ini][0][0], edgeCuts[mid][0][0]);
  var equalUp = sortedArrayEquals(edgeCuts[end][0][0], edgeCuts[mid][0][0]);
  if (!equalDown) calcCuts(cuts, edgeCuts, edges, ini, mid);
  if (!equalUp) calcCuts(cuts, edgeCuts, edges, mid, end);
}

function sortedArrayEquals(a, b) {
  for (var i = 0; i < a.length; i++) if (a[i] != b[i]) return false;
  return true;
}

function addEdge(nodes, n1, n2, w, i) {
  if (nodes[n1 - 1] == undefined) nodes[n1 - 1] = [];
  if (nodes[n2 - 1] == undefined) nodes[n2 - 1] = [];

  nodes[n1 - 1].push([n2 - 1, w, i]);
  nodes[n2 - 1].push([n1 - 1, w, i]);
}

function mst(edges, nodes, q) {
  edges.clear();
  var visited = { 0: true };
  var nVisited = 1;
  var edgesOrder = [];
  var weightsOrder = [];
  var sum = 0;
  for (var edge of nodes[0])
    edges.add([edge[0], Math.abs(edge[1] - q), edge[2], edge[1]]);
  while (true) {
    var min = edges.pop();
    var node = min[0];
    if (!visited[node]) {
      visited[node] = true;
      edgesOrder.push(min[2]);
      weightsOrder.push(min[3]);
      sum += min[1];
      if (++nVisited == nodes.length) break;
      for (var edge of nodes[node]) {
        if (!visited[edge[0]])
          edges.add([edge[0], Math.abs(edge[1] - q), edge[2], edge[1]]);
      }
    }
  }
  return [
    edgesOrder.sort((a, b) => a - b),
    weightsOrder.sort((a, b) => a - b),
    sum
  ];
}

function PriorityQueue(compare) {
  this.heap = [];
  this.elements = 0;
  this.compare = compare || ((a, b) => a < b);
}

PriorityQueue.prototype.size = function () {
  return this.elements;
};

PriorityQueue.prototype.isEmpty = function () {
  return this.elements == 0;
};

PriorityQueue.prototype.clear = function () {
  this.elements = 0;
};

PriorityQueue.prototype.peek = function () {
  return this.elements > 0 ? heap[0] : undefined;
};

PriorityQueue.prototype.add = function (myVal) {
  this.heap[this.elements++] = myVal;

  var current = this.elements - 1;
  while (current > 0) {
    var up = (current - 1) >> 1;
    var upVal = this.heap[up];
    if (this.compare(upVal, myVal)) break;
    this.heap[current] = upVal;
    current = up;
  }
  this.heap[current] = myVal;
};

PriorityQueue.prototype.pop = function () {
  if (this.elements == 0) return null;
  if (this.elements == 1) {
    return this.heap[--this.elements];
  }
  var el = this.heap[0];

  var myVal = this.heap[--this.elements];
  this.heap[0] = myVal;

  var current = 0;
  var maxSize = this.elements / 2;
  var leftChild;
  var rightChild;
  var best;

  while (current < maxSize) {
    leftChild = current * 2 + 1;
    rightChild = leftChild + 1;
    best = this.heap[leftChild];

    if (
      rightChild < this.elements &&
      this.compare(this.heap[rightChild], best)
    ) {
      best = this.heap[rightChild];
      leftChild = rightChild;
    }
    if (this.compare(myVal, best)) {
      break;
    }
    this.heap[current] = best;
    current = leftChild;
  }
  this.heap[current] = myVal;

  return el;
};

PriorityQueue.prototype.toArray = function () {
  return this.heap.filter((_, id) => id < this.elements);
};

var line = readline().split(" ");
var n = parseInt(line[0]);
var m = parseInt(line[1]);

var nodes = Array(n);
var weights = Array(m);
var iniEdges = Array(m);
for (var i = 0; i < m; i++) {
  var edge = readline().split(" ");
  var node1 = parseInt(edge[0]);
  var node2 = parseInt(edge[1]);
  var weight = parseInt(edge[2]) * 2; //*2 to avoid decimals
  addEdge(nodes, node1, node2, weight, i);
  weights[i] = weight;
  iniEdges[i] = [node1, node2, parseInt(edge[2])];
}

var cuts = new Set();
cuts.add(0);
for (var i = 0; i < m; i++) {
  var w1 = weights[i];
  cuts.add(w1);
  for (var j = 0; j < i; j++) {
    cuts.add((w1 + weights[j]) / 2);
  }
}
cuts = Array.from(cuts);
cuts.sort((a, b) => a - b);

var line = readline().split(" ");
var p = parseInt(line[0]);
var k = parseInt(line[1]);
var a = parseInt(line[2]);
var b = parseInt(line[3]);
var c = parseInt(line[4]);

var queries = readline()
  .split(" ")
  .map((i) => parseInt(i));

var edges = new PriorityQueue((a, b) => {
  return a[1] == b[1] ? b[3] < a[3] : a[1] < b[1];
});

var edgesCuts = new Array(cuts.length);
edgesCuts[0] = [mst(edges, nodes, cuts[0]), cuts[0]];
edgesCuts[cuts.length - 1] = [
  mst(edges, nodes, cuts[cuts.length - 1]),
  cuts[cuts.length - 1]
];
if (!sortedArrayEquals(edgesCuts[0][0], edgesCuts[cuts.length - 1][0]))
  calcCuts(cuts, edgesCuts, edges, 0, cuts.length - 1);
edgesCuts = edgesCuts.filter((i) => i != undefined);
edgesCuts = edgesCuts.filter(
  (i, id) => id == 0 || !sortedArrayEquals(i[0], edgesCuts[id - 1][0])
);

var dict = {};
var dictCuts = {};
var result = 0;
var q = 0;
for (var i = 0; i < k; i++) {
  var qbase = i < queries.length ? queries[i] : ((q * a) / 2 + b) % c;
  q = qbase * 2;
  if (dict[q] == undefined) {
    var j = upperBound(edgesCuts, q);

    var edgesList = edgesCuts[j][0];
    var sum = 0;
    for (var edge of edgesList) {
      sum += Math.abs(iniEdges[edge][2] - qbase);
    }
    dict[q] = sum;
  }
  result ^= dict[q];
}

print(result);

function upperBound(edgesCuts, q) {
  var min = 0;
  var max = edgesCuts.length - 1;
  var mid = Math.floor((min + max) / 2);
  while (min != mid) {
    if (edgesCuts[mid][1] < q) min = mid;
    else max = mid;
    mid = Math.floor((min + max) / 2);
  }
  return mid;
}

function calcCuts(cuts, edgesCuts, edges, ini, end) {
  var mid = Math.floor((ini + end) / 2);
  if (mid == ini) return;
  edgesCuts[mid] = [mst(edges, nodes, cuts[mid]), cuts[mid]];
  var equalDown = sortedArrayEquals(edgesCuts[ini][0], edgesCuts[mid][0]);
  var equalUp = sortedArrayEquals(edgesCuts[end][0], edgesCuts[mid][0]);
  if (!equalDown) calcCuts(cuts, edgesCuts, edges, ini, mid);
  if (!equalUp) calcCuts(cuts, edgesCuts, edges, mid, end);
}

function sortedArrayEquals(a, b) {
  for (var i = 0; i < a.length; i++) if (a[i] != b[i]) return false;
  return true;
}

function addEdge(nodes, n1, n2, w, i) {
  if (nodes[n1 - 1] == undefined) nodes[n1 - 1] = [];
  if (nodes[n2 - 1] == undefined) nodes[n2 - 1] = [];

  nodes[n1 - 1].push([n2 - 1, w, i]);
  nodes[n2 - 1].push([n1 - 1, w, i]);
}

function mst(edges, nodes, q) {
  edges.clear();
  var visited = { 0: true };
  var nVisited = 1;
  var edgesOrder = [];
  for (var edge of nodes[0])
    edges.add([edge[0], Math.abs(edge[1] - q), edge[2], edge[1]]);
  while (true) {
    var min = edges.pop();
    var node = min[0];
    if (!visited[node]) {
      visited[node] = true;
      edgesOrder.push(min[2]);
      if (++nVisited == nodes.length) break;
      for (var edge of nodes[node]) {
        if (!visited[edge[0]])
          edges.add([edge[0], Math.abs(edge[1] - q), edge[2], edge[1]]);
      }
    }
  }
  return edgesOrder.sort();
}

function PriorityQueue(compare) {
  this.heap = [];
  this.elements = 0;
  this.compare = compare || ((a, b) => a < b);
}

PriorityQueue.prototype.size = function () {
  return this.elements;
};

PriorityQueue.prototype.isEmpty = function () {
  return this.elements == 0;
};

PriorityQueue.prototype.clear = function () {
  this.elements = 0;
};

PriorityQueue.prototype.peek = function () {
  return this.elements > 0 ? heap[0] : undefined;
};

PriorityQueue.prototype.add = function (myVal) {
  this.heap[this.elements++] = myVal;

  var current = this.elements - 1;
  while (current > 0) {
    var up = (current - 1) >> 1;
    var upVal = this.heap[up];
    if (this.compare(upVal, myVal)) break;
    this.heap[current] = upVal;
    current = up;
  }
  this.heap[current] = myVal;
};

PriorityQueue.prototype.pop = function () {
  if (this.elements == 0) return null;
  if (this.elements == 1) {
    return this.heap[--this.elements];
  }
  var el = this.heap[0];

  var myVal = this.heap[--this.elements];
  this.heap[0] = myVal;

  var current = 0;
  var maxSize = this.elements / 2;
  var leftChild;
  var rightChild;
  var best;

  while (current < maxSize) {
    leftChild = current * 2 + 1;
    rightChild = leftChild + 1;
    best = this.heap[leftChild];

    if (
      rightChild < this.elements &&
      this.compare(this.heap[rightChild], best)
    ) {
      best = this.heap[rightChild];
      leftChild = rightChild;
    }
    if (this.compare(myVal, best)) {
      break;
    }
    this.heap[current] = best;
    current = leftChild;
  }
  this.heap[current] = myVal;

  return el;
};

PriorityQueue.prototype.toArray = function () {
  return this.heap.filter((_, id) => id < this.elements);
};

var line = readline().split(" ");
var n = parseInt(line[0]);
var m = parseInt(line[1]);

var nodes = Array(n);
var weights = Array(m);
var iniEdges = Array(m);
for (var i = 0; i < m; i++) {
  var edge = readline().split(" ");
  var node1 = parseInt(edge[0]);
  var node2 = parseInt(edge[1]);
  var weight = parseInt(edge[2]) * 2; //*2 to avoid decimals
  addEdge(nodes, node1, node2, weight, i);
  weights[i] = weight;
  iniEdges[i] = [node1, node2, parseInt(edge[2])];
}

var cuts = new Set();
cuts.add(0);
for (var i = 0; i < m; i++) {
  var w1 = weights[i];
  cuts.add(w1);
  for (var j = 0; j < i; j++) {
    cuts.add((w1 + weights[j]) / 2);
  }
}
cuts = Array.from(cuts);
cuts.sort((a, b) => a - b);

var line = readline().split(" ");
var p = parseInt(line[0]);
var k = parseInt(line[1]);
var a = parseInt(line[2]);
var b = parseInt(line[3]);
var c = parseInt(line[4]);

var queries = readline()
  .split(" ")
  .map((i) => parseInt(i));

var edges = new PriorityQueue((a, b) => {
  return a[1] == b[1] ? b[3] < a[3] : a[1] < b[1];
});

var edgesCuts = new Array(cuts.length);
edgesCuts[0] = [mst(edges, nodes, cuts[0]), cuts[0]];
edgesCuts[cuts.length - 1] = [
  mst(edges, nodes, cuts[cuts.length - 1]),
  cuts[cuts.length - 1]
];
if (!sortedArrayEquals(edgesCuts[0][0], edgesCuts[cuts.length - 1][0]))
  calcCuts(cuts, edgesCuts, edges, 0, cuts.length - 1);
edgesCuts = edgesCuts.filter((i) => i != undefined);
edgesCuts = edgesCuts.filter(
  (i, id) => id == 0 || !sortedArrayEquals(i[0], edgesCuts[id - 1][0])
);

var dict = {};
var dictCuts = {};
var result = 0;
var q = 0;
for (var i = 0; i < k; i++) {
  var qbase = i < queries.length ? queries[i] : ((q * a) / 2 + b) % c;
  q = qbase * 2;
  if (dict[q] == undefined) {
    var jmin = 0;
    var jmax = edgesCuts.length - 1;

    var j = Math.floor((jmin + jmax) / 2);
    while (jmin != j) {
      if (edgesCuts[j][1] <= q) jmin = j;
      else jmax = j;
      j = Math.floor((jmin + jmax) / 2);
    }

    var edgesList = edgesCuts[j][0];
    var sum = 0;
    for (var edge of edgesList) {
      sum += Math.abs(iniEdges[edge][2] - qbase);
    }
    dict[q] = sum;
  }
  result ^= dict[q];
}

print(result);

function calcCuts(cuts, edgesCuts, edges, ini, end) {
  var mid = Math.floor((ini + end) / 2);
  if (mid == ini) return;
  edgesCuts[mid] = [mst(edges, nodes, cuts[mid]), cuts[mid]];
  var equalDown = sortedArrayEquals(edgesCuts[ini][0], edgesCuts[mid][0]);
  var equalUp = sortedArrayEquals(edgesCuts[end][0], edgesCuts[mid][0]);
  if (!equalDown) calcCuts(cuts, edgesCuts, edges, ini, mid);
  if (!equalUp) calcCuts(cuts, edgesCuts, edges, mid, end);
}

function sortedArrayEquals(a, b) {
  return a.join() == b.join();
}

function addEdge(nodes, n1, n2, w, i) {
  if (nodes[n1 - 1] == undefined) nodes[n1 - 1] = [];
  if (nodes[n2 - 1] == undefined) nodes[n2 - 1] = [];

  nodes[n1 - 1].push([n2 - 1, w, i]);
  nodes[n2 - 1].push([n1 - 1, w, i]);
}

function mst(edges, nodes, q) {
  edges.clear();
  var visited = { 0: true };
  var nVisited = 1;
  var edgesOrder = [];
  for (var edge of nodes[0])
    edges.add([edge[0], Math.abs(edge[1] - q), edge[2], edge[1]]);
  while (true) {
    var min = edges.pop();
    var node = min[0];
    if (!visited[node]) {
      visited[node] = true;
      edgesOrder.push(min[2]);
      if (++nVisited == nodes.length) break;
      for (var edge of nodes[node]) {
        if (!visited[edge[0]])
          edges.add([edge[0], Math.abs(edge[1] - q), edge[2], edge[1]]);
      }
    }
  }
  return edgesOrder.sort();
}

function PriorityQueue(compare) {
  this.heap = [];
  this.elements = 0;
  this.compare = compare || ((a, b) => a < b);
}

PriorityQueue.prototype.size = function () {
  return this.elements;
};

PriorityQueue.prototype.isEmpty = function () {
  return this.elements == 0;
};

PriorityQueue.prototype.clear = function () {
  this.elements = 0;
};

PriorityQueue.prototype.peek = function () {
  return this.elements > 0 ? heap[0] : undefined;
};

PriorityQueue.prototype.add = function (myVal) {
  this.heap[this.elements++] = myVal;

  var current = this.elements - 1;
  while (current > 0) {
    var up = (current - 1) >> 1;
    var upVal = this.heap[up];
    if (this.compare(upVal, myVal)) break;
    this.heap[current] = upVal;
    current = up;
  }
  this.heap[current] = myVal;
};

PriorityQueue.prototype.pop = function () {
  if (this.elements == 0) return null;
  if (this.elements == 1) {
    return this.heap[--this.elements];
  }
  var el = this.heap[0];

  var myVal = this.heap[--this.elements];
  this.heap[0] = myVal;

  var current = 0;
  var maxSize = this.elements / 2;
  var leftChild;
  var rightChild;
  var best;

  while (current < maxSize) {
    leftChild = current * 2 + 1;
    rightChild = leftChild + 1;
    best = this.heap[leftChild];

    if (
      rightChild < this.elements &&
      this.compare(this.heap[rightChild], best)
    ) {
      best = this.heap[rightChild];
      leftChild = rightChild;
    }
    if (this.compare(myVal, best)) {
      break;
    }
    this.heap[current] = best;
    current = leftChild;
  }
  this.heap[current] = myVal;

  return el;
};

PriorityQueue.prototype.toArray = function () {
  return this.heap.filter((_, id) => id < this.elements);
};

var line = readline().split(" ");
var n = parseInt(line[0]);
var m = parseInt(line[1]);

var nodes = Array(n);
var weights = Array(m);
var iniEdges = Array(m);
for (var i = 0; i < m; i++) {
  var edge = readline().split(" ");
  var node1 = parseInt(edge[0]);
  var node2 = parseInt(edge[1]);
  var weight = parseInt(edge[2]) * 2; //*2 to avoid decimals
  addEdge(nodes, node1, node2, weight, i);
  weights[i] = weight;
  iniEdges[i] = [node1, node2, weight];
}

var cuts = new Set();
cuts.add(0);
for (var i = 0; i < m; i++) {
  var w1 = weights[i];
  cuts.add(w1);
  for (var j = 0; j < i; j++) {
    cuts.add((w1 + weights[j]) / 2);
  }
}
cuts = Array.from(cuts);
cuts.sort((a, b) => a - b);

var line = readline().split(" ");
var p = parseInt(line[0]);
var k = parseInt(line[1]);
var a = parseInt(line[2]);
var b = parseInt(line[3]);
var c = parseInt(line[4]);

var queries = readline()
  .split(" ")
  .map((i) => parseInt(i));

var edges = new PriorityQueue((a, b) => {
  return a[1] == b[1] ? b[3] < a[3] : a[1] < b[1];
});

var edgesCuts = new Array(cuts.length);
edgesCuts[0] = [mst(edges, nodes, cuts[0])[2], cuts[0]];
edgesCuts[cuts.length - 1] = [
  mst(edges, nodes, cuts[cuts.length - 1])[2],
  cuts[cuts.length - 1]
];
if (!sortedArrayEquals(edgesCuts[0][0], edgesCuts[cuts.length - 1][0]))
  calcCuts(cuts, edgesCuts, edges, 0, cuts.length - 1);
edgesCuts = edgesCuts.filter((i) => i != undefined);

var dict = {};
var dictCuts = {};
var result = 0;
var q = 0;
for (var i = 0; i < k; i++) {
  q = i < queries.length ? queries[i] * 2 : (((q * a) / 2 + b) % c) * 2;
  if (dict[q] == undefined) {
    var j = 0;

    while (j < edgesCuts.length && edgesCuts[j][1] <= q) {
      j++;
    }
    j--;
    var edgesList = edgesCuts[j][0];
    var sum = 0;
    for (var edge of edgesList) {
      sum += Math.abs(iniEdges[edge][2] - q);
    }
    dict[q] = sum / 2;
  }
  result ^= dict[q];
}
// for (var i = 0; i < k; i++) {
//   q = i < queries.length ? queries[i] * 2 : (((q * a) / 2 + b) % c) * 2;
//   if (dict[q] == undefined) {
//     var j = 0;

//     while (j < cuts.length && cuts[j] <= q) {
//       j++;
//     }
//     j--;
//     var prevSol = dictCuts[cuts[j]];
//     if (prevSol == undefined) {
//       dictCuts[cuts[j]] = mst(edges, nodes, cuts[j]);
//       prevSol = dictCuts[cuts[j]];
//     }
//     var sol = (prevSol[0] + (n - 1 - 2 * prevSol[1]) * (q - cuts[j])) / 2;
//     dict[q] = sol;
//   }
//   result ^= dict[q];
// }

print(result);

function calcCuts(cuts, edgesCuts, edges, ini, end) {
  var mid = Math.floor((ini + end) / 2);
  if (mid == ini) return;
  edgesCuts[mid] = [mst(edges, nodes, cuts[mid])[2], cuts[mid]];
  var equalDown = sortedArrayEquals(edgesCuts[ini][0], edgesCuts[mid][0]);
  var equalUp = sortedArrayEquals(edgesCuts[end][0], edgesCuts[mid][0]);
  if (!equalDown) calcCuts(cuts, edgesCuts, edges, ini, mid);
  if (!equalUp) calcCuts(cuts, edgesCuts, edges, mid, end);
  if (equalDown || equalUp) delete edgesCuts[mid];
}

function sortedArrayEquals(a, b) {
  return a.join() == b.join();
}

function addEdge(nodes, n1, n2, w, i) {
  if (nodes[n1 - 1] == undefined) nodes[n1 - 1] = [];
  if (nodes[n2 - 1] == undefined) nodes[n2 - 1] = [];

  nodes[n1 - 1].push([n2 - 1, w, i]);
  nodes[n2 - 1].push([n1 - 1, w, i]);
}

function mst(edges, nodes, q) {
  edges.clear();
  var sum = 0;
  var edgesUp = 0;
  var visited = { 0: true };
  var nVisited = 1;
  var edgesOrder = [];
  for (var edge of nodes[0])
    edges.add([edge[0], Math.abs(edge[1] - q), edge[2], edge[1]]);
  //var eds = [];
  while (true) {
    var min = edges.pop();
    var node = min[0];
    if (!visited[node]) {
      visited[node] = true;
      sum += min[1];
      edgesOrder.push(min[2]);
      if (q < min[3]) {
        edgesUp++;
      }
      //  eds.push(min[2]); // Don't know why this makes the code run faster
      if (++nVisited == nodes.length) break;
      for (var edge of nodes[node]) {
        if (!visited[edge[0]])
          edges.add([edge[0], Math.abs(edge[1] - q), edge[2], edge[1]]);
      }
    }
  }
  return [sum, edgesUp, edgesOrder.sort()];
}

function PriorityQueue(maxSize, compare) {
  this.maxSize = maxSize || 100000;
  this.heap = new Array(this.maxSize);
  this.elements = 0;
  this.compare = compare || ((a, b) => a - b);
}

PriorityQueue.prototype.size = function () {
  return this.elements;
};

PriorityQueue.prototype.isEmpty = function () {
  return this.elements == 0;
};

PriorityQueue.prototype.clear = function () {
  this.elements = 0;
};

PriorityQueue.prototype.peek = function () {
  return this.elements > 0 ? heap[0] : undefined;
};

PriorityQueue.prototype.add = function (myVal) {
  this.heap[this.elements++] = myVal;

  var current = this.elements - 1;
  while (current > 0) {
    var up = (current - 1) >> 1;
    var upVal = this.heap[up];
    if (this.compare(myVal, upVal) >= 0) break;
    this.heap[current] = upVal;
    current = up;
  }
  this.heap[current] = myVal;
};

PriorityQueue.prototype.pop = function () {
  if (this.elements == 0) return null;
  if (this.elements == 1) {
    return this.heap[--this.elements];
  }
  var el = this.heap[0];

  var myVal = this.heap[--this.elements];
  this.heap[0] = myVal;

  var current = 0;
  var maxSize = this.elements >> 1;
  var leftChild;
  var rightChild;
  var best;

  while (current < maxSize) {
    leftChild = (current << 1) + 1;
    rightChild = leftChild + 1;
    best = this.heap[leftChild];

    if (
      rightChild < this.elements &&
      this.compare(best, this.heap[rightChild]) > 0
    ) {
      best = this.heap[rightChild];
      leftChild = rightChild;
    }
    if (this.compare(best, myVal) >= 0) {
      break;
    }
    this.heap[current] = best;
    current = leftChild;
  }
  this.heap[current] = myVal;

  return el;
};

PriorityQueue.prototype.toArray = function () {
  return this.heap.filter((_, id) => id < this.elements);
};

var line = readline().split(" ");
var n = parseInt(line[0]);
var m = parseInt(line[1]);

var nodes = Array(n);
var weights = Array(m);
for (var i = 0; i < m; i++) {
  var edge = readline().split(" ");
  var node1 = parseInt(edge[0]);
  var node2 = parseInt(edge[1]);
  var weight = parseInt(edge[2]) << 1; //*2 to avoid decimals
  addEdge(nodes, node1, node2, weight, i);
  weights[i] = weight;
}

var cuts = new Set();
cuts.add(0);
for (var i = 0; i < m; i++) {
  var w1 = weights[i];
  cuts.add(w1);
  for (var j = 0; j < i; j++) {
    cuts.add((w1 + weights[j]) >> 1);
  }
}
cuts = Array.from(cuts);
cuts.sort((a, b) => a - b);

var line = readline().split(" ");
var p = parseInt(line[0]);
var k = parseInt(line[1]);
var a = parseInt(line[2]);
var b = parseInt(line[3]);
var c = parseInt(line[4]);

var queries = readline()
  .split(" ")
  .map((i) => parseInt(i));
var max = queries.reduce((a, b) => Math.max(a, b), 0);
max = Math.max(max, c) << 1;

var dictCuts = {};

var dict = {};
var result = 0;
var q = 0;
for (var i = 0; i < k; i++) {
  q = i < queries.length ? queries[i] << 1 : (((q * a) >> 1) + b) % c << 1;
  if (dict[q] == undefined) {
    var j = 0;
    while (j < cuts.length && cuts[j] <= q) {
      j++;
    }
    j--;
    var prevSol = dictCuts[cuts[j]];
    if (prevSol == undefined) {
      dictCuts[cuts[j]] = mst(nodes, cuts[j]);
      prevSol = dictCuts[cuts[j]];
    }
    var sol = (prevSol[0] + (n - 1 - 2 * prevSol[1]) * (q - cuts[j])) >> 1;
    dict[q] = sol;
  }
  result ^= dict[q];
  if (i % 100000 == 0) print(i / 100000);
}

print(result);

function addEdge(nodes, n1, n2, w, i) {
  if (nodes[n1 - 1] == undefined) nodes[n1 - 1] = [];
  if (nodes[n2 - 1] == undefined) nodes[n2 - 1] = [];

  nodes[n1 - 1].push([n2 - 1, w, i]);
  nodes[n2 - 1].push([n1 - 1, w, i]);
}

function mst(nodes, q) {
  var edges = new PriorityQueue(400, (a, b) => {
    var res = a[1] - b[1];
    return res == 0 ? b[3] - a[3] : res;
  });
  var sum = 0;
  var edgesUp = 0;
  var visited = { 0: true };
  var nVisited = 1;
  for (var edge of nodes[0])
    edges.add([edge[0], Math.abs(edge[1] - q), edge[2], edge[1]]);
  var eds = [];
  while (nVisited < nodes.length) {
    var min = edges.pop();
    var node = min[0];
    if (!visited[node]) {
      visited[node] = true;
      nVisited++;
      sum += min[1];
      eds.push(min[2]); // Don't know why this makes the code run faster
      if (q < min[3]) {
        edgesUp++;
      }
      for (var edge of nodes[node]) {
        if (!visited[edge[0]])
          edges.add([edge[0], Math.abs(edge[1] - q), edge[2], edge[1]]);
      }
    }
  }
  return [sum, edgesUp];
}

function PriorityQueue(maxSize, compare) {
  this.maxSize = maxSize || 100000;
  this.heap = new Array(this.maxSize);
  this.elements = 0;
  this.compare = compare || ((a, b) => a - b);
}

PriorityQueue.prototype.size = function () {
  return this.elements;
};

PriorityQueue.prototype.isEmpty = function () {
  return this.elements == 0;
};

PriorityQueue.prototype.clear = function () {
  this.elements = 0;
};

PriorityQueue.prototype.peek = function () {
  return this.elements > 0 ? heap[0] : undefined;
};

PriorityQueue.prototype.add = function (myVal) {
  this.heap[this.elements++] = myVal;

  var current = this.elements - 1;
  while (current > 0) {
    var up = (current - 1) >> 1;
    var upVal = this.heap[up];
    if (this.compare(myVal, upVal) >= 0) break;
    this.heap[current] = upVal;
    current = up;
  }
  this.heap[current] = myVal;
};

PriorityQueue.prototype.pop = function () {
  if (this.elements == 0) return null;
  if (this.elements == 1) {
    return this.heap[--this.elements];
  }
  var el = this.heap[0];

  var myVal = this.heap[--this.elements];
  this.heap[0] = myVal;

  var current = 0;
  var maxSize = this.elements >> 1;
  var leftChild;
  var rightChild;
  var best;

  while (current < maxSize) {
    leftChild = (current << 1) + 1;
    rightChild = leftChild + 1;
    best = this.heap[leftChild];

    if (
      rightChild < this.elements &&
      this.compare(best, this.heap[rightChild]) > 0
    ) {
      best = this.heap[rightChild];
      leftChild = rightChild;
    }
    if (this.compare(best, myVal) >= 0) {
      break;
    }
    this.heap[current] = best;
    current = leftChild;
  }
  this.heap[current] = myVal;

  return el;
};

PriorityQueue.prototype.toArray = function () {
  return this.heap.filter((_, id) => id < this.elements);
};

var line = readline().split(" ");
var n = parseInt(line[0]);
var m = parseInt(line[1]);

var nodes = Array(n);
var weights = Array(m);
for (var i = 0; i < m; i++) {
  var edge = readline().split(" ");
  var node1 = parseInt(edge[0]);
  var node2 = parseInt(edge[1]);
  var weight = parseInt(edge[2]) << 1; //*2 to avoid decimals
  addEdge(nodes, node1, node2, weight, i);
  weights[i] = weight;
}

var cuts = new Set();
cuts.add(0);
for (var i = 0; i < m; i++) {
  var w1 = weights[i];
  cuts.add(w1);
  for (var j = 0; j < i; j++) {
    cuts.add((w1 + weights[j]) >> 1);
  }
}
cuts = Array.from(cuts);
cuts.sort((a, b) => a - b);

var line = readline().split(" ");
var p = parseInt(line[0]);
var k = parseInt(line[1]);
var a = parseInt(line[2]);
var b = parseInt(line[3]);
var c = parseInt(line[4]);

var queries = readline()
  .split(" ")
  .map((i) => parseInt(i));
var max = queries.reduce((a, b) => Math.max(a, b), 0);
max = Math.max(max, c) << 1;

var dictCuts = {};

var dict = {};
var result = 0;
var q = 0;
for (var i = 0; i < k; i++) {
  q = i < queries.length ? queries[i] << 1 : (((q * a) >> 1) + b) % c << 1;
  if (dict[q] == undefined) {
    var j = 0;
    while (j < cuts.length && cuts[j] <= q) {
      j++;
    }
    j--;
    var prevSol = dictCuts[cuts[j]];
    if (prevSol == undefined) {
      dictCuts[cuts[j]] = mst(nodes, cuts[j]);
      prevSol = dictCuts[cuts[j]];
    }
    var sol = (prevSol[0] + (n - 1 - 2 * prevSol[1]) * (q - cuts[j])) >> 1;
    dict[q] = sol;
  }
  result ^= dict[q];
  if (n > 17 && nodes[17][0][1] == 51480 * 2 && i % 10000 == 0) print(i / 1000);
}

print(result);

function addEdge(nodes, n1, n2, w, i) {
  if (nodes[n1 - 1] == undefined) nodes[n1 - 1] = [];
  if (nodes[n2 - 1] == undefined) nodes[n2 - 1] = [];

  nodes[n1 - 1].push([n2 - 1, w, i]);
  nodes[n2 - 1].push([n1 - 1, w, i]);
}

function mst(nodes, q) {
  var edges = new PriorityQueue(400, (a, b) => {
    var res = a[1] - b[1];
    return res == 0 ? b[3] - a[3] : res;
  });
  var sum = 0;
  var edgesUp = 0;
  var visited = { 0: true };
  var nVisited = 1;
  for (var edge of nodes[0])
    edges.add([edge[0], Math.abs(edge[1] - q), edge[2], edge[1]]);
  var eds = [];
  while (nVisited < nodes.length) {
    var min = edges.pop();
    var node = min[0];
    if (!visited[node]) {
      visited[node] = true;
      nVisited++;
      sum += min[1];
      eds.push(min[2]); // Don't know why this makes the code run faster
      if (q < min[3]) {
        edgesUp++;
      }
      for (var edge of nodes[node]) {
        if (!visited[edge[0]])
          edges.add([edge[0], Math.abs(edge[1] - q), edge[2], edge[1]]);
      }
    }
  }
  return [sum, edgesUp];
}

function PriorityQueue(maxSize, compare) {
  this.maxSize = maxSize || 100000;
  this.heap = new Array(this.maxSize);
  this.elements = 0;
  this.compare = compare || ((a, b) => a - b);
}

PriorityQueue.prototype.size = function () {
  return this.elements;
};

PriorityQueue.prototype.isEmpty = function () {
  return this.elements == 0;
};

PriorityQueue.prototype.clear = function () {
  this.elements = 0;
};

PriorityQueue.prototype.peek = function () {
  return this.elements > 0 ? heap[0] : undefined;
};

PriorityQueue.prototype.add = function (myVal) {
  this.heap[this.elements++] = myVal;

  var current = this.elements - 1;
  while (current > 0) {
    var up = (current - 1) >> 1;
    var upVal = this.heap[up];
    if (this.compare(myVal, upVal) >= 0) break;
    this.heap[current] = upVal;
    current = up;
  }
  this.heap[current] = myVal;
};

PriorityQueue.prototype.pop = function () {
  if (this.elements == 0) return null;
  if (this.elements == 1) {
    return this.heap[--this.elements];
  }
  var el = this.heap[0];

  var myVal = this.heap[--this.elements];
  this.heap[0] = myVal;

  var current = 0;
  var maxSize = this.elements >> 1;
  var leftChild;
  var rightChild;
  var best;

  while (current < maxSize) {
    leftChild = (current << 1) + 1;
    rightChild = leftChild + 1;
    best = this.heap[leftChild];

    if (
      rightChild < this.elements &&
      this.compare(best, this.heap[rightChild]) > 0
    ) {
      best = this.heap[rightChild];
      leftChild = rightChild;
    }
    if (this.compare(best, myVal) >= 0) {
      break;
    }
    this.heap[current] = best;
    current = leftChild;
  }
  this.heap[current] = myVal;

  return el;
};

PriorityQueue.prototype.toArray = function () {
  return this.heap.filter((_, id) => id < this.elements);
};

var line = readline().split(" ");
var n = parseInt(line[0]);
var m = parseInt(line[1]);

var nodes = Array(n);
var weights = Array(m);
for (var i = 0; i < m; i++) {
  var edge = readline().split(" ");
  var node1 = parseInt(edge[0]);
  var node2 = parseInt(edge[1]);
  var weight = parseInt(edge[2]) * 2; //*2 to avoid decimals
  addEdge(nodes, node1, node2, weight, i);
  weights[i] = weight;
}

var cuts = new Set();
cuts.add(0);
for (var i = 0; i < m; i++) {
  var w1 = weights[i];
  for (var j = 0; j < i; j++) {
    cuts.add((w1 + weights[j]) >> 1);
  }
}
cuts = Array.from(cuts);
cuts.sort((a, b) => a - b);

var dictCuts = {};
for (var cut of cuts) {
  dictCuts[cut] = mst(nodes, cut);
}

var line = readline().split(" ");
var p = parseInt(line[0]);
var k = parseInt(line[1]);
var a = parseInt(line[2]);
var b = parseInt(line[3]);
var c = parseInt(line[4]);

var queries = readline()
  .split(" ")
  .map((i) => parseInt(i));

var dict = {};
var result = 0;
var q = 0;
for (var i = 0; i < k; i++) {
  q = i < queries.length ? 2 * queries[i] : (((q * a) / 2 + b) % c) * 2;
  if (dict[q] == undefined) {
    var j = 0;
    while (j < cuts.length && cuts[j] <= q) {
      j++;
    }
    j--;
    var prevSol = dictCuts[cuts[j]];
    var sol = (prevSol[0] + (n - 1 - 2 * prevSol[1]) * (q - cuts[j])) / 2;
    dict[q] = sol;
  }
  result ^= dict[q];
}

print(result);

function addEdge(nodes, n1, n2, w, i) {
  if (nodes[n1 - 1] == undefined) nodes[n1 - 1] = [];
  if (nodes[n2 - 1] == undefined) nodes[n2 - 1] = [];

  nodes[n1 - 1].push([n2 - 1, w, i]);
  nodes[n2 - 1].push([n1 - 1, w, i]);
}

function mst(nodes, q) {
  var edges = new PriorityQueue(400, (a, b) => {
    var res = a[1] - b[1];
    return res == 0 ? b[3] - a[3] : res;
  });
  var sum = 0;
  var edgesUp = 0;
  var visited = new Set();
  visited.add(0);
  var nVisited = 1;
  for (var edge of nodes[0])
    edges.add([edge[0], Math.abs(edge[1] - q), edge[2], edge[1]]);
  while (nVisited < nodes.length) {
    var min = edges.pop();
    var node = min[0];
    if (!visited.has(node)) {
      visited.add(node);
      nVisited++;
      sum += min[1];
      if (q < min[3]) {
        edgesUp++;
      }
      for (var edge of nodes[node]) {
        if (!visited.has(edge[0]))
          edges.add([edge[0], Math.abs(edge[1] - q), edge[2], edge[1]]);
      }
    }
  }
  return [sum, edgesUp];
}

function PriorityQueue(maxSize, compare) {
  this.maxSize = maxSize || 100000;
  this.heap = new Array(this.maxSize);
  this.elements = 0;
  this.compare = compare || ((a, b) => a - b);
}

PriorityQueue.prototype.size = function () {
  return this.elements;
};

PriorityQueue.prototype.isEmpty = function () {
  return this.elements == 0;
};

PriorityQueue.prototype.clear = function () {
  this.elements = 0;
};

PriorityQueue.prototype.peek = function () {
  return this.elements > 0 ? heap[0] : undefined;
};

PriorityQueue.prototype.add = function (myVal) {
  this.heap[this.elements++] = myVal;

  var current = this.elements - 1;
  while (current > 0) {
    var up = (current - 1) >> 1;
    var upVal = this.heap[up];
    if (this.compare(myVal, upVal) >= 0) break;
    this.heap[current] = upVal;
    current = up;
  }
  this.heap[current] = myVal;
};

PriorityQueue.prototype.pop = function () {
  if (this.elements == 0) return null;
  if (this.elements == 1) {
    return this.heap[--this.elements];
  }
  var el = this.heap[0];

  var myVal = this.heap[--this.elements];
  this.heap[0] = myVal;

  var current = 0;
  var maxSize = this.elements >> 1;
  var leftChild;
  var rightChild;
  var best;

  while (current < maxSize) {
    leftChild = (current << 1) + 1;
    rightChild = leftChild + 1;
    best = this.heap[leftChild];

    if (
      rightChild < this.elements &&
      this.compare(best, this.heap[rightChild]) > 0
    ) {
      best = this.heap[rightChild];
      leftChild = rightChild;
    }
    if (this.compare(best, myVal) >= 0) {
      break;
    }
    this.heap[current] = best;
    current = leftChild;
  }
  this.heap[current] = myVal;

  return el;
};

PriorityQueue.prototype.toArray = function () {
  return this.heap.filter((_, id) => id < this.elements);
};

var line = readline().split(" ");
var n = parseInt(line[0]);
var m = parseInt(line[1]);

var nodes = Array(n);
var weights = Array(m);
for (var i = 0; i < m; i++) {
  var edge = readline().split(" ");
  var node1 = parseInt(edge[0]);
  var node2 = parseInt(edge[1]);
  var weight = parseInt(edge[2]) * 2; //*2 to avoid decimals
  addEdge(nodes, node1, node2, weight, i);
  weights[i] = weight;
}

var cuts = new Set();
cuts.add(0);
for (var i = 0; i < m; i++) {
  var w1 = weights[i];
  for (var j = 0; j < i; j++) {
    cuts.add((w1 + weights[j]) >> 1);
  }
}
cuts = Array.from(cuts);
cuts.sort((a, b) => a - b);

var dictCuts = {};
for (var cut of cuts) {
  dictCuts[cut] = mst(nodes, cut);
}

var line = readline().split(" ");
var p = parseInt(line[0]);
var k = parseInt(line[1]);
var a = parseInt(line[2]);
var b = parseInt(line[3]);
var c = parseInt(line[4]);

var queries = readline()
  .split(" ")
  .map((i) => parseInt(i));

for (var i = p; i < k; i++) {
  queries.push((queries[i - 1] * a + b) % c);
}

var dict = {};
var result = 0;
var q = 0;
for (var i = 0; i < k; i++) {
  q = i < queries.length ? 2 * queries[i] : (((q * a) / a + b) % c) * 2;
  if (dict[q] == undefined) {
    var j = 0;
    while (j < cuts.length && cuts[j] <= q) {
      j++;
    }
    j--;
    var prevSol = dictCuts[cuts[j]];
    var sol = (prevSol[0] + (n - 1 - 2 * prevSol[1]) * (q - cuts[j])) / 2;
    dict[q] = sol;
  }
  result ^= dict[q];
}

print(result);

function addEdge(nodes, n1, n2, w, i) {
  if (nodes[n1 - 1] == undefined) nodes[n1 - 1] = [];
  if (nodes[n2 - 1] == undefined) nodes[n2 - 1] = [];

  nodes[n1 - 1].push([n2 - 1, w, i]);
  nodes[n2 - 1].push([n1 - 1, w, i]);
}

function mst(nodes, q) {
  var edges = new PriorityQueue(400, (a, b) => {
    var res = a[1] - b[1];
    return res == 0 ? b[3] - a[3] : res;
  });
  var sum = 0;
  var edgesUp = 0;
  var visited = new Set();
  visited.add(0);
  var nVisited = 1;
  for (var edge of nodes[0])
    edges.add([edge[0], Math.abs(edge[1] - q), edge[2], edge[1]]);
  while (nVisited < nodes.length) {
    var min = edges.pop();
    var node = min[0];
    if (!visited.has(node)) {
      visited.add(node);
      nVisited++;
      sum += min[1];
      if (q < min[3]) {
        edgesUp++;
      }
      for (var edge of nodes[node]) {
        if (!visited.has(edge[0]))
          edges.add([edge[0], Math.abs(edge[1] - q), edge[2], edge[1]]);
      }
    }
  }
  return [sum, edgesUp];
}

function PriorityQueue(maxSize, compare) {
  this.maxSize = maxSize || 100000;
  this.heap = new Array(this.maxSize);
  this.elements = 0;
  this.compare = compare || ((a, b) => a - b);
}

PriorityQueue.prototype.size = function () {
  return this.elements;
};

PriorityQueue.prototype.isEmpty = function () {
  return this.elements == 0;
};

PriorityQueue.prototype.clear = function () {
  this.elements = 0;
};

PriorityQueue.prototype.peek = function () {
  return this.elements > 0 ? heap[0] : undefined;
};

PriorityQueue.prototype.insert = function (myVal) {
  this.heap[this.elements++] = myVal;

  var current = this.elements - 1;
  while (current > 0) {
    var up = (current - 1) >> 1;
    var upVal = this.heap[up];
    if (!this.compare(myVal, upVal)) break;
    this.heap[current] = upVal;
    current = up;
  }
  this.heap[current] = myVal;
};

PriorityQueue.prototype.pop = function () {
  if (this.elements == 0) return null;
  var el = this.heap[0];

  this.heap[0] = this.heap[this.elements - 1];
  this.elements--;

  var current = 0;
  var leftChild = current * 2 + 1;
  var rightChild = current * 2 + 2;
  var currentVal = this.heap[current];
  var leftChildVal = this.heap[leftChild];
  var rightChildVal = this.heap[rightChild];

  while (
    rightChild < this.elements &&
    (this.compare(currentVal, leftChildVal) > 0 ||
      this.compare(currentVal, rightChildVal) > 0)
  ) {
    if (this.compare(leftChildVal, rightChildVal) > 0) {
      this.heap[rightChild] = currentVal;
      this.heap[current] = rightChildVal;
      current = rightChild;
    } else {
      this.heap[leftChild] = currentVal;
      this.heap[current] = leftChildVal;
      current = leftChild;
    }
    leftChild = current * 2 + 1;
    rightChild = current * 2 + 2;
    currentVal = this.heap[current];
    leftChildVal = this.heap[leftChild];
    rightChildVal = this.heap[rightChild];
  }

  if (
    rightChild == this.elements &&
    this.compare(currentVal, leftChildVal) > 0
  ) {
    this.heap[leftChild] = currentVal;
    this.heap[current] = leftChildVal;
  }

  return el;
};

PriorityQueue.prototype.toArray = function () {
  return this.heap.filter((_, id) => id < this.elements);
};

PriorityQueue.prototype.toArray = function () {
  return this.heap.filter((_, id) => id < this.elements);
};

var line = readline().split(" ");
var n = parseInt(line[0]);
var m = parseInt(line[1]);

var nodes = Array(n);
var rrr = false;
var aaa = {};
var bbb = 0;
for (var i = 0; i < m; i++) {
  var edge = readline().split(" ");
  var node1 = parseInt(edge[0]);
  var node2 = parseInt(edge[1]);
  var weight = parseInt(edge[2]);
  if (weight == 51480) rrr = true;
  if (rrr) {
    if (aaa[node1 + "," + node2] == undefined) aaa[node1 + "," + node2] = true;
    else bbb++;
  }
  addEdge(nodes, node1, node2, weight, i);
}

if (rrr) print(bbb);

var line = readline().split(" ");
var p = parseInt(line[0]);
var k = parseInt(line[1]);
var a = parseInt(line[2]);
var b = parseInt(line[3]);
var c = parseInt(line[4]);

var queries = readline()
  .split(" ")
  .map((i) => parseInt(i));

for (var i = p; i < k; i++) {
  queries.push((queries[i - 1] * a + b) % c);
}

var dict = {};
var results = [];
for (var i = 0; i < queries.length; i++) {
  q = queries[i];
  if (dict[q] == undefined) dict[q] = mst(nodes, q);
  if (n > 17 && nodes[17][0][1] == 51480 && i % 1000 == 0)
    print(i / 1000 + "," + Object.keys(dict).length);
  results.push(dict[q]);
}

print(results.reduce((a, b) => a ^ b, 0));

function addEdge(nodes, n1, n2, w, i) {
  if (nodes[n1 - 1] == undefined) nodes[n1 - 1] = [];
  if (nodes[n2 - 1] == undefined) nodes[n2 - 1] = [];

  nodes[n1 - 1].push([n2 - 1, w, i]);
  nodes[n2 - 1].push([n1 - 1, w, i]);
}

function mst(nodes, q) {
  var edges = new PriorityQueue(400, (a, b) => a[1] - b[1]);
  var sum = 0;
  var visited = { 0: true };
  var nVisited = 1;
  for (var edge of nodes[0])
    edges.insert([edge[0], Math.abs(edge[1] - q), edge[2]]);
  var eds = [];
  while (nVisited < nodes.length) {
    var min = edges.pop();
    var node = min[0];
    if (!visited[node]) {
      visited[node] = true;
      nVisited++;
      sum += min[1];
      eds.push(min[2]);
      for (var edge of nodes[node]) {
        if (!visited[edge[0]])
          edges.insert([edge[0], Math.abs(edge[1] - q), edge[2]]);
      }
    }
  }
  return sum;
}

function PriorityQueue(maxSize, compare) {
  this.maxSize = maxSize || 100000;
  this.heap = new Array(this.maxSize);
  this.elements = 0;
  this.compare = compare || ((a, b) => a - b);
}

PriorityQueue.prototype.size = function () {
  return this.elements;
};

PriorityQueue.prototype.isEmpty = function () {
  return this.elements == 0;
};

PriorityQueue.prototype.clear = function () {
  this.elements = 0;
};

PriorityQueue.prototype.peek = function () {
  return this.elements > 0 ? heap[0] : undefined;
};

PriorityQueue.prototype.insert = function (e) {
  if (this.elements == this.maxSize) throw "Error max size exceeded";
  this.heap[this.elements++] = e;

  var current = this.elements - 1;
  while (
    current > 0 &&
    this.compare(this.heap[Math.floor((current - 1) / 2)], this.heap[current]) >
      0
  ) {
    var tmp = this.heap[Math.floor((current - 1) / 2)];
    this.heap[Math.floor((current - 1) / 2)] = this.heap[current];
    this.heap[current] = tmp;
    current = Math.floor((current - 1) / 2);
  }
};

PriorityQueue.prototype.pop = function () {
  if (this.elements == 0) return null;
  var el = this.heap[0];

  this.heap[0] = this.heap[this.elements - 1];
  this.elements--;

  var current = 0;
  var leftChild = current * 2 + 1;
  var rightChild = current * 2 + 2;
  var currentVal = this.heap[current];
  var leftChildVal = this.heap[leftChild];
  var rightChildVal = this.heap[rightChild];

  while (
    rightChild < this.elements &&
    (this.compare(currentVal, leftChildVal) > 0 ||
      this.compare(currentVal, rightChildVal) > 0)
  ) {
    if (this.compare(leftChildVal, rightChildVal) > 0) {
      this.heap[rightChild] = currentVal;
      this.heap[current] = rightChildVal;
      current = rightChild;
    } else {
      this.heap[leftChild] = currentVal;
      this.heap[current] = leftChildVal;
      current = leftChild;
    }
    leftChild = current * 2 + 1;
    rightChild = current * 2 + 2;
    currentVal = this.heap[current];
    leftChildVal = this.heap[leftChild];
    rightChildVal = this.heap[rightChild];
  }

  if (
    rightChild == this.elements &&
    this.compare(currentVal, leftChildVal) > 0
  ) {
    this.heap[leftChild] = currentVal;
    this.heap[current] = leftChildVal;
  }

  return el;
};

PriorityQueue.prototype.toArray = function () {
  return this.heap.filter((_, id) => id < this.elements);
};

var line = readline().split(" ");
var n = parseInt(line[0]);
var m = parseInt(line[1]);

var nodes = Array(n);
var iniEdges = [];
for (var i = 0; i < m; i++) {
  var edge = readline().split(" ");
  var node1 = parseInt(edge[0]);
  var node2 = parseInt(edge[1]);
  var weight = parseInt(edge[2]);
  addEdge(iniEdges, nodes, node1, node2, weight, i);
}

var line = readline().split(" ");
var p = parseInt(line[0]);
var k = parseInt(line[1]);
var a = parseInt(line[2]);
var b = parseInt(line[3]);
var c = parseInt(line[4]);

print(JSON.stringify(iniEdges) + ","+p+ ","+k+ ","+a+ ","+b+","+c)

var queries = readline()
  .split(" ")
  .map((i) => parseInt(i));

for (var i = p; i < k; i++) {
  queries.push((queries[i - 1] * a + b) % c);
}

var dict = {};
var results = [];
var dictOrder = {};
for (var i = 0; i < queries.length; i++) {
  q = queries[i];
  if (dict[q] == undefined) dict[q] = mst(dictOrder, iniEdges, nodes, q);
  results.push(dict[q]);
}

print(results.reduce((a, b) => a ^ b, 0));

function addEdge(iniEdges, nodes, n1, n2, w, i) {
  if (nodes[n1 - 1] == undefined) nodes[n1 - 1] = [];
  if (nodes[n2 - 1] == undefined) nodes[n2 - 1] = [];
  nodes[n1 - 1].push([n2 - 1, w, i]);
  nodes[n2 - 1].push([n1 - 1, w, i]);
  iniEdges.push([i, n1, n2, w]);
}

function mst(dictOrder, iniEdges, nodes, q) {
  var edges = new PriorityQueue(400, (a, b) => a[1] - b[1]);
  var iniEdges2 = iniEdges.map((i) => [i[0], i[1], i[2], Math.abs(i[3] - q)]);
  iniEdges2.sort((a, b) => a[3] - b[3]);
  var key = iniEdges2.map((i) => i[0]).join(",");

  var sum = 0;
  if (dictOrder[key] == undefined) {
    var visited = { 0: true };
    var nVisited = 1;
    for (var edge of nodes[0])
      edges.insert([edge[0], Math.abs(edge[1] - q), edge[2]]);
    var eds = [];
    while (nVisited < nodes.length) {
      var min = edges.pop();
      var node = min[0];
      if (!visited[node]) {
        visited[node] = true;
        nVisited++;
        sum += min[1];
        eds.push(min[2]);
        for (var edge of nodes[node]) {
          if (!visited[edge[0]])
            edges.insert([edge[0], Math.abs(edge[1] - q), edge[2]]);
        }
      }
    }
    dictOrder[key] = eds;
  } else {
    for (el of dictOrder[key]) sum += Math.abs(iniEdges[el][3] - q);
  }
  return sum;
}

var line = readline().split(" ");
var n = parseInt(line[0]);
var m = parseInt(line[1]);

var nodes = Array(n);
for (var i = 0; i < m; i++) {
  var edge = readline().split(" ");
  var node1 = parseInt(edge[0]);
  var node2 = parseInt(edge[1]);
  var weight = parseInt(edge[2]);
  addEdge(nodes, node1, node2, weight);
}

var line = readline().split(" ");
var p = parseInt(line[0]);
var k = parseInt(line[1]);
var a = parseInt(line[2]);
var b = parseInt(line[3]);
var c = parseInt(line[4]);

var queries = readline()
  .split(" ")
  .map((i) => parseInt(i));

for (var i = p; i < k; i++) {
  queries.push((queries[i - 1] * a + b) % c);
}

var dict = {};
var results = [];
for (var i = 0; i < queries.length; i++) {
  q = queries[i];
  if (dict[q] == undefined) dict[q] = mst(nodes, q);
  results.push(dict[q]);
}

print(
  results.reduce((a, b) => a ^ b),
  0
);

function addEdge(nodes, n1, n2, w) {
  if (nodes[n1 - 1] == undefined) nodes[n1 - 1] = [];
  if (nodes[n2 - 1] == undefined) nodes[n2 - 1] = [];
  nodes[n1 - 1].push([n2 - 1, w]);
  nodes[n2 - 1].push([n1 - 1, w]);
}

function mst(nodes, q) {
  var nodes2 = nodes.map((i) => i.map((j) => [j[0], Math.abs(j[1] - q)]));
  var edges = nodes2[0];
  var sum = 0;
  var vertexs = new Set();
  vertexs.add(0);
  while (edges.length > 0) {
    var min = edges.reduce((a, b) => (a[1] <= b[1] ? a : b), [0, Infinity]);
    sum += min[1];
    vertexs.add(min[0]);
    edges = edges.concat(nodes2[min[0]]);
    edges = edges.filter((i) => !vertexs.has(i[0]));
  }
  return sum;
}


var str = readline();
var len = str.length;

function work(type, beg, end) {
    var arr = new Array(len);
    for (var i = 0; i < len; ++i) {
        arr[i] = Number(str[i]);
    }
    var ans = new Array(len);
    var down = false;

    while (true) {
        if (down) {
            if (arr[end] !== 0) {
                --arr[end];
                down = false;
            } else {
                arr[end] = 9;
                down = true;
            }
        }
        if (beg === end) {
            if (down) {
                return false;
            }
            if (type === 1) {
                if (arr[end] % 2 === 0) {
                    ans[end] = String(arr[end] / 2);
                    return ans;
                } else {
                    return false;
                }
            } else {
                if (arr[end] % 2 === 0) {
                    ans[end] = String((arr[end] + 10) / 2);
                    return ans;
                } else {
                    return false;
                }
            }
        }
        if (beg +1 === end) {
            if (down) {
                if (arr[beg] !== 0) {
                    --arr[beg];
                    down = false;
                } else {
                   arr[beg] = 9;
                    down = true;
                }
            }
            if (down) {
                if (type === 1) {
                    return false;
                } else {
                    ans[beg] = "9";
                    ans[end] = "0";
                    return ans;
                }
            }
            if (type === 1) {
                if (arr[beg] === arr[end]) {
                    ans[beg] = String(arr[end]);
                    ans[end] = "0";
                    return ans;
                } else {
                    return false;
                }
            } else {
                if (arr[beg] === arr[end] + 1) {
                    ans[beg] = "9";
                    ans[end] = String(arr[end] + 1);
                    return ans;
                } else {
                    return false;
                }
            }
        }
        if (type === 1) {
            if (arr[beg] === arr[end]) {
                type = 1;
                ans[beg] = String(arr[end]);
                ans[end] = "0";
                ++beg, --end;
            } else if (arr[beg] === arr[end] + 1) {
                type = 2;
                ans[beg] = String(arr[end]);
                if (beg === 0 && ans[beg] === "0") {
                    return false;
                }
                ans[end] = "0";
                ++beg, --end;
            } else {
                return false;
            }
        } else {
            if (arr[beg] === arr[end]) {
                if (arr[beg] !== 9) {
                    type = 1;
                    ans[beg] = "9";
                    ans[end] = String(arr[end] + 1);
                    down = true;
                    ++beg, --end;
                } else {
                    return false;
                }
            } else if (arr[beg] === arr[end] + 1) {
                type = 2;
                ans[beg] = "9";
                ans[end] = String(arr[end] + 1);
                down = true;
                ++beg, --end;
            } else if (arr[beg] === 0 && arr[end] === 9) {
                type = 2;
                ans[beg] = "9";
                ans[end] = "0";
                ++beg, --end;
            } else {
                return false;
            }
        }
    }
}

var ans = work(1, 0, len - 1);
if (ans) {
    print(ans.join(""));
} else {
    if (str[0] === "1") {
        ans = work(2, 1, len - 1);
        if (ans) {
            print(ans.join(""));
        } else {
            print(0);
        }
    } else {
        print(0);
    }
}

var str = readline();
var len = str.length;

function work(type, beg, end) {
    var arr = new Array(len);
    for (var i = 0; i < len; ++i) {
        arr[i] = Number(str[i]);
    }
    var ans = new Array(len);
    var down = false;

    while (true) {
        if (beg > end) {
            if (type === 1 && !down || type === 2 && down) {
                return ans;
            } else {
                return false;
            }
        }
        if (down) {
            if (arr[end] !== 0) {
                --arr[end];
                down = false;
            } else {
                arr[end] = 9;
                down = true;
            }
        }
        if (beg === end) {
            if (down) {
                return false;
            }
            if (type === 1) {
                if (arr[end] % 2 === 0) {
                    ans[end] = String(arr[end] / 2);
                    return ans;
                } else {
                    return false;
                }
            } else {
                if (arr[end] % 2 === 0) {
                    ans[end] = String((arr[end] + 10) / 2);
                    return ans;
                } else {
                    return false;
                }
            }
        }
        if (type === 1) {
            if (arr[beg] === arr[end]) {
                type = 1;
                ans[beg] = String(arr[end]);
                ans[end] = "0";
                ++beg, --end;
            } else if (arr[beg] === arr[end] + 1) {
                type = 2;
                ans[beg] = String(arr[end]);
                if (beg === 0 && ans[beg] === "0") {
                    return false;
                }
                ans[end] = "0";
                ++beg, --end;
            } else {
                return false;
            }
        } else {
            if (arr[beg] === arr[end]) {
                if (arr[beg] !== 9) {
                    type = 1;
                    ans[beg] = "9";
                    ans[end] = String(arr[end] + 1);
                    down = true;
                    ++beg, --end;
                } else {
                    return false;
                }
            } else if (arr[beg] === arr[end] + 1) {
                type = 2;
                ans[beg] = "9";
                ans[end] = String(arr[end] + 1);
                down = true;
                ++beg, --end;
            } else if (arr[beg] === 0 && arr[end] === 9) {
                type = 2;
                ans[beg] = "9";
                ans[end] = "0";
                ++beg, --end;
            } else {
                return false;
            }
        }
    }
}

var ans = work(1, 0, len - 1);
if (ans) {
    print(ans.join(""));
} else {
    if (str[0] === "1") {
        ans = work(2, 1, len - 1);
        if (ans) {
            print(ans.join(""));
        } else {
            print(0);
        }
    } else {
        print(0);
    }
}

var str = readline();
var len = str.length;

function work(type, beg, end) {
    var arr = new Array(len);
    for (var i = 0; i < len; ++i) {
        arr[i] = Number(str[i]);
    }
    var ans = new Array(len);
    var down = false;

    while (true) {
        if (down) {
            if (arr[end] !== 0) {
                --arr[end];
                down = false;
            } else {
                arr[end] = 9;
                down = true;
            }
        }
        if (beg === end) {
            if (down) {
                return false;
            }
            if (type === 1) {
                if (arr[end] % 2 === 0) {
                    ans[end] = String(arr[end] / 2);
                    return ans;
                } else {
                    return false;
                }
            } else {
                if (arr[end] % 2 === 0) {
                    ans[end] = String((arr[end] + 10) / 2);
                    return ans;
                } else {
                    return false;
                }
            }
        }
        if (beg +1 === end) {
            if (down) {
                if (arr[beg] !== 0) {
                    --arr[beg];
                    down = false;
                } else {
                   arr[beg] = 9;
                    down = true;
                }
            }
            if (type === 1 && down || type === 2 && !down) {
                return false;
            }
            if (arr[beg] === arr[end]) {
                ans[beg] = String(arr[end]);
                ans[end] = "0";
                return ans;
            } else {
                return false;
            }
        }
        if (type === 1) {
            if (arr[beg] === arr[end]) {
                type = 1;
                ans[beg] = String(arr[end]);
                ans[end] = "0";
                ++beg, --end;
            } else if (arr[beg] === arr[end] + 1) {
                type = 2;
                ans[beg] = String(arr[end]);
                if (beg === 0 && ans[beg] === "0") {
                    return false;
                }
                ans[end] = "0";
                ++beg, --end;
            } else {
                return false;
            }
        } else {
            if (arr[beg] === arr[end]) {
                if (arr[beg] !== 9) {
                    type = 1;
                    ans[beg] = "9";
                    ans[end] = String(arr[end] + 1);
                    down = true;
                    ++beg, --end;
                } else {
                    return false;
                }
            } else if (arr[beg] === arr[end] + 1) {
                type = 2;
                ans[beg] = "9";
                ans[end] = String(arr[end] + 1);
                down = true;
                ++beg, --end;
            } else if (arr[beg] === 0 && arr[end] === 9) {
                type = 2;
                ans[beg] = "9";
                ans[end] = "0";
                ++beg, --end;
            } else {
                return false;
            }
        }
    }
}

var ans = work(1, 0, len - 1);
if (ans) {
    print(ans.join(""));
} else {
    if (str[0] === "1") {
        ans = work(2, 1, len - 1);
        if (ans) {
            print(ans.join(""));
        } else {
            print(0);
        }
    } else {
        print(0);
    }
}

var str = readline();
var len = str.length;

function work(type, beg, end) {
    var arr = new Array(len);
    for (var i = 0; i < len; ++i) {
        arr[i] = Number(str[i]);
    }
    var ans = new Array(len);
    var down = false;

    while (true) {
        if (down) {
            if (arr[end] !== 0) {
                --arr[end];
                down = false;
            } else {
                arr[end] = 9;
                down = true;
            }
        }
        if (beg > end) {
            if (type === 1 && !down || type === 2 && down) {
                return ans;
            } else {
                return false;
            }
        } else if (beg === end) {
            if (down) {
                return false;
            }
            if (type === 1) {
                if (arr[end] % 2 === 0) {
                    ans[end] = String(arr[end] / 2);
                    return ans;
                } else {
                    return false;
                }
            } else {
                if (arr[end] % 2 === 0) {
                    ans[end] = String((arr[end] + 10) / 2);
                    return ans;
                } else {
                    return false;
                }
            }
        }
        if (type === 1) {
            if (arr[beg] === arr[end]) {
                type = 1;
                ans[beg] = String(arr[end]);
                ans[end] = "0";
                ++beg, --end;
            } else if (arr[beg] === arr[end] + 1) {
                type = 2;
                ans[beg] = String(arr[end]);
                if (beg === 0 && ans[beg] === "0") {
                    return false;
                }
                ans[end] = "0";
                ++beg, --end;
            } else {
                return false;
            }
        } else {
            if (arr[beg] === arr[end]) {
                if (arr[beg] !== 9) {
                    type = 1;
                    ans[beg] = "9";
                    ans[end] = String(arr[end] + 1);
                    down = true;
                    ++beg, --end;
                } else {
                    return false;
                }
            } else if (arr[beg] === arr[end] + 1) {
                type = 2;
                ans[beg] = "9";
                ans[end] = String(arr[end] + 1);
                down = true;
                ++beg, --end;
            } else if (arr[beg] === 0 && arr[end] === 9) {
                type = 2;
                ans[beg] = "9";
                ans[end] = "0";
                ++beg, --end;
            } else {
                return false;
            }
        }
    }
}

var ans = work(1, 0, len - 1);
if (ans) {
    print(ans.join(""));
} else {
    if (str[0] === "1") {
        ans = work(2, 1, len - 1);
        if (ans) {
            print(ans.join(""));
        } else {
            print(0);
        }
    } else {
        print(0);
    }
}

var str = readline();
var len = str.length;

function work(type, beg, end) {
    var arr = new Array(len);
    for (var i = 0; i < len; ++i) {
        arr[i] = Number(str[i]);
    }
    var ans = new Array(len);
    var down = false;

    while (true) {
        if (down) {
            if (arr[end] !== 0) {
                --arr[end];
                down = false;
            } else {
                arr[end] = 9;
                down = true;
            }
        }
        if (beg > end) {
            if (type === 1 && !down || type === 2 && down) {
                return ans;
            } else {
                return false;
            }
        } else if (beg === end) {
            if (down) {
                return false;
            }
            if (type === 1) {
                if (arr[end] % 2 === 0) {
                    ans[end] = String(arr[end] / 2);
                    return ans;
                } else {
                    return false;
                }
            } else {
                if (arr[end] % 2 === 0) {
                    ans[end] = String((arr[end] + 10) / 2);
                    return ans;
                } else {
                    return false;
                }
            }
        }
        if (type === 1) {
            if (arr[beg] === arr[end]) {
                type = 1;
                ans[beg] = String(arr[end]);
                ans[end] = "0";
                ++beg, --end;
            } else if (arr[beg] === arr[end] + 1) {
                type = 2;
                ans[beg] = String(arr[end]);
                ans[end] = "0";
                ++beg, --end;
            } else {
                return false;
            }
        } else {
            if (arr[beg] === arr[end]) {
                if (arr[beg] !== 9) {
                    type = 1;
                    ans[beg] = "9";
                    ans[end] = String(arr[end] + 1);
                    down = true;
                    ++beg, --end;
                } else {
                    return false;
                }
            } else if (arr[beg] === arr[end] + 1) {
                type = 2;
                ans[beg] = "9";
                ans[end] = String(arr[end] + 1);
                down = true;
                ++beg, --end;
            } else if (arr[beg] === 0 && arr[end] === 9) {
                type = 2;
                ans[beg] = "9";
                ans[end] = "0";
                ++beg, --end;
            } else {
                return false;
            }
        }
    }
}

var ans = work(1, 0, len - 1);
if (ans) {
    print(ans.join(""));
} else {
    if (str[0] === "1") {
        ans = work(2, 1, len - 1);
        if (ans) {
            print(ans.join(""));
        } else {
            print(0);
        }
    } else {
        print(0);
    }
}


print(function(h, m) {

	for (var x = 1; x <= 10; x++) {
		if (!h[x]) continue;
		var ans = [x];
		var a = [x, 0];
		var last = x;
		for (var i = 1; i < m; i++) {
			for (var j = 1; j <= 10; j++)
				if (h[j] && j !== last && a[i % 2] + j > a[(i + 1) % 2]) {
					a[i % 2] += j;
					last = j;
					ans.push(j);
					break;
				}

			if (j === 11)
				break;
		}
		if (ans.length === m) {
			return 'YES\n' + ans.join(' ');
		}
	}

	return 'NO';

}(('0' + readline()).split('').map(function(v) {
	return v === '1';
}), +readline()));

print(function(h, m) {
	var q = [];

	function d(step, x) {
		if (step === m) {
			return true;
		}
		for (var i = 1; i <= 10; i++) {
			var y = step % 2 === 1 ? x + i : x - i;
			q[step] = i;
			if (h[i] && i !== q[step - 1] && ((x >= 0 && y < 0) || (x <= 0 && y > 0)) && d(step + 1, y)) {
				return true;
			}
		}

		return false;
	}

	var ans = d(0, 0);
	if (ans) return 'YES\n' + q.join(' ');
	return 'NO';

}(('0' + readline()).split('').map(function(v) {
	return v === '1';
}), +readline()));

print(function(h, m) {

	var ans = [];
	var a = [0, 0];
	var last = 0;
	for (var i = 0; i < m; i++) {
		for (var j = 1; j <= 10; j++)
			if (h[j] && j !== last && a[i % 2] + j > a[(i + 1) % 2]) {
				a[i % 2] += j;
				last = j;
				ans.push(j);
				break;
			}

		if (j === 11)
			return 'NO';
	}
	return 'YES\n' + ans.join(' ');

}(('0' + readline()).split('').map(function(v) {
	return v === '1';
}), +readline()));