#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int x;
    cin >> x;
    int n = s.length();
    string t = "";
    for (int i = 0; i < n; i++) t += "1";
    for (int i = 0; i < n; i++)
      if (s[i] == '0') {
        if (i - x >= 0) t[i - x] = '0';
        if (i + x < n) t[i + x] = '0';
      }
    bool can = true;
    for (int i = 0; i < n; i++)
      if (s[i] == '1') {
        bool ok = false;
        ok |= i - x >= 0 && t[i - x] == '1';
        ok |= i + x < n && t[i + x] == '1';
        can &= ok;
      }
    if (can) {
      cout << t << "\n";
    } else {
      cout << "-1\n";
    }
  }
  return 0;
}
