#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int n, m, u, v, color, q, ans[N][N];
vector<pair<int, int> > nei[N];
bool mark[N];
void dfs(int v, int color) {
  mark[v] = true;
  for (int i = 0; i < nei[v].size(); i++) {
    if (!mark[nei[v][i].first] && nei[v][i].second == color) {
      dfs(nei[v][i].first, color);
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> u >> v >> color;
    u--, v--, color--;
    nei[u].push_back(make_pair(v, color));
    nei[v].push_back(make_pair(u, color));
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      memset(mark, false, sizeof(mark));
      dfs(i, j);
      mark[i] = false;
      for (int k = 0; k < n; k++) {
        if (mark[k]) ans[i][k]++;
      }
    }
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> u >> v;
    u--, v--;
    cout << ans[u][v] << endl;
  }
  return 0;
}
