#include <bits/stdc++.h>
using namespace std;
int main() {
  int T = 1;
  cin >> T;
  while (T--) {
    string s;
    int x;
    cin >> s >> x;
    string ans;
    int n = s.size();
    for (int i = 0; i < n; i++) ans.push_back('1');
    for (int i = 0; i < n; i++) {
      if (s[i] == '0') {
        if (i - x >= 0) ans[i - x] = '0';
        if (i + x < n) ans[i + x] = '0';
      }
    }
    bool f = 1;
    for (int i = 0; i < n; i++) {
      if (s[i] == '1') {
        bool t = 0;
        if (i + x < n && ans[i + x] == '1') t = 1;
        if (i - x >= 0 && ans[i - x] == '1') t = 1;
        if (!t) f = 0;
      }
    }
    if (f)
      cout << ans << endl;
    else
      cout << "-1\n";
  }
  return 0;
}
