#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
  long long int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long int x;
    cin >> x;
    long long int n = (long long int)s.length();
    vector<int> v(n, 2);
    for (long long int i = x; i < n - x; i++) {
      if (s[i - x] == '1' && s[i + x] == '1')
        v[i] = 1;
      else
        v[i] = 0;
    }
    for (long long int i = 0; i < x; i++) {
      if ((i + x) < n && s[i + x] == '0')
        v[i] = 0;
      else
        v[i] = 1;
    }
    for (long long int i = n - x; i < n; i++) {
      if ((i - x) >= 0 && s[i - x] == '0')
        v[i] = 0;
      else
        v[i] = 1;
    }
    string ans;
    for (long long int i = 0; i < n; i++) {
      if (i - x >= 0 && v[i - x] == 1)
        ans += "1";
      else if (i + x < n && v[i + x] == 1)
        ans += "1";
      else
        ans += "0";
    }
    if (ans != s)
      cout << "-1\n";
    else {
      string a;
      for (long long int i = 0; i < n; i++) a += ('0' + v[i]);
      cout << a << "\n";
    }
  }
}
