#include <bits/stdc++.h>
using namespace std;
bool func(long long n) {
  n = abs(n);
  while (n != 0) {
    if (n % 10 == 8) return true;
    n = n / 10;
  }
  return false;
}
long long gcd(long long a, long long b) {
  while (a != 0) {
    long long temp = a;
    a = b % a;
    b = temp;
  }
  return b;
}
int32_t main() {
  long long t;
  cin >> t;
  while (t-- != 0) {
    string s;
    cin >> s;
    long long x;
    cin >> x;
    string w = string(s.size(), '1');
    bool m = true;
    for (long long i = 1; i < s.size() + 1; i++) {
      if (s[i - 1] == '0') {
        if (i > x) w[i - 1 - x] = '0';
        if (i + x <= s.size()) w[i + x - 1] = '0';
      }
    }
    for (long long i = 1; i < s.size() + 1; i++) {
      if (i > x and w[i - x - 1] == '1') {
        if (s[i - 1] != '1') {
          cout << -1 << endl;
          m = false;
          break;
        }
      }
      if (i + x <= s.size() and w[i + x - 1] == '1') {
        if (s[i - 1] != '1') {
          cout << -1 << endl;
          m = false;
          break;
        }
      }
      if (!(i > x and w[i - x - 1] == '1') and
          !(i + x <= s.size() and w[i + x - 1] == '1')) {
        if (s[i - 1] != '0') {
          cout << -1 << endl;
          m = false;
          break;
        }
      }
    }
    if (m) cout << w << endl;
  }
  return 0;
}
