#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e7;
const int N = 2e6 + 13, M = N;
int n, m;
void solve() {
  string s;
  long long x;
  cin >> s >> x;
  int n = s.size(), i;
  string w(n, '0');
  bool ok = true;
  for (i = 0; i < n; i++) {
    if (s[i] == '1') {
      ok = false;
      if (i + x < n) {
        if (i + x + x >= n || s[i + x + x] == '1') {
          w[i + x] = '1';
          ok = 1;
        }
      }
      if (i - x >= 0) {
        if (i - x - x < 0 || s[i - x - x] == '1') {
          w[i - x] = '1';
          ok = 1;
        }
      }
      if (!ok) break;
    }
  }
  string ans = "-1";
  if (ok) {
    ans = w;
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
