#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, n, i, flag, x, y;
  vector<int> w;
  cin >> t;
  y = 0;
  while (t--) {
    y++;
    string s;
    cin >> s;
    cin >> x;
    i = 0;
    while (s[i] != '\0') {
      i++;
    }
    n = i;
    w.clear();
    w.resize(n, -1);
    i = 0;
    while (s[i] != '\0') {
      if (s[i] == '0') {
        if (i + x < n) w[i + x] = 0;
        if (i - x > -1) w[i - x] = 0;
      }
      i++;
    }
    i = 0;
    flag = 0;
    while (s[i] != '\0') {
      if (s[i] == '1') {
        if (i + x < n && i - x > -1) {
          if (w[i + x] == 0 && w[i - x] == 0) {
            flag = 1;
            break;
          }
        } else if (i + x < n) {
          if (w[i + x] == 0) {
            flag = 1;
            break;
          }
        } else if (i - x > -1) {
          if (w[i - x] == 0) {
            flag = 1;
            break;
          }
        } else {
          flag = 1;
          break;
        }
      }
      i++;
    }
    if (flag == 0) {
      for (i = 0; i < n; i++) {
        if (w[i] == -1)
          cout << 1;
        else
          cout << w[i];
      }
      cout << endl;
    } else {
      cout << "-1" << endl;
    }
  }
  return 0;
}
