#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int x;
    cin >> x;
    string w = string(s.length(), '2');
    bool ok = 1;
    for (int i = 0; i < s.length(); i++) {
      if (i - x < 0 && i + x >= (int)s.length() && s[i] == '1') {
        ok = 0;
        break;
      }
    }
    for (int i = 0; i < s.length(); i++) {
      if (i - x >= 0 && i + x >= (int)s.length()) w[i - x] = s[i];
    }
    for (int i = 0; i < s.length(); i++) {
      if (i - x < 0 && i + x < (int)s.length()) {
        if ((s[i] - '0') + (w[i + x] - '0') == 1) {
          ok = 0;
          break;
        }
        w[i + x] = s[i];
      }
    }
    for (int i = 0; i < s.length(); i++) {
      if (i - x >= 0 && i + x < (int)s.length()) {
        if (s[i] == '0') {
          if (w[i + x] == '1' || w[i - x] == '1') {
            ok = 0;
            break;
          }
          w[i + x] = w[i - x] = '0';
        } else {
          if (w[i - x] != '0')
            w[i - x] = '1';
          else if (w[i + x] != '0')
            w[i + x] = '1';
          else {
            ok = 0;
            break;
          }
        }
      }
    }
    if (!ok)
      cout << "-1\n";
    else {
      for (int i = 0; i < w.length(); i++) {
        w[i] = min(w[i], '1');
      }
      cout << w << "\n";
    }
  }
}
