#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long tests;
  cin >> tests;
  while (tests--) {
    string s;
    cin >> s;
    long long x;
    cin >> x;
    string ans(s.size(), '?');
    function<bool(long long)> valid = [&](long long ind) {
      return (ind >= 0 and ind < s.size());
    };
    bool ok = true;
    for (long long j = 0; j < 2; j++)
      for (long long i = 0; i < s.size(); i++) {
        if (!valid(i - x) and !valid(i + x)) {
          if (s[i] == '1') {
            ok = false;
            break;
          }
        }
        if (!valid(i - x) and valid(i + x)) {
          if (ans[i + x] == '?')
            ans[i + x] = s[i];
          else if (ans[i + x] != s[i]) {
            ok = false;
            break;
          }
        }
        if (valid(i - x) and !valid(i + x)) {
          if (ans[i - x] == '?')
            ans[i - x] = s[i];
          else if (ans[i - x] != s[i]) {
            ok = false;
            break;
          }
        }
        if (valid(i + x) and valid(i - x)) {
          if (ans[i - x] == '?' and ans[i + x] == '?') {
            if (s[i] == '0') {
              ans[i - x] = ans[i + x] = s[i];
            }
          }
          if (ans[i - x] != '?' and ans[i + x] == '?') {
            if (s[i] == '1') {
              if (ans[i - x] == '0') ans[i + x] = '1';
            }
            if (s[i] == '0') {
              if (ans[i - x] == '0')
                ans[i + x] = '0';
              else if (ans[i - x] == '1') {
                ok = false;
                break;
              }
            }
          }
          if (ans[i - x] == '?' and ans[i + x] != '?') {
            if (s[i] == '1') {
              if (ans[i + x] == '0') ans[i - x] = '1';
            }
            if (s[i] == '0') {
              if (ans[i + x] == '0')
                ans[i - x] = '0';
              else if (ans[i + x] == '1') {
                ok = false;
                break;
              }
            }
          }
        }
      }
    if (!ok) {
      cout << -1 << '\n';
    } else {
      for (char ch : ans) {
        if (ch == '?')
          cout << '1';
        else
          cout << ch;
      }
      cout << '\n';
    }
  }
  return 0;
}
